资讯专栏INFORMATION COLUMN

java-工具类Collections和Arrays的设计和区别

mj / 2044人阅读

摘要:排序的算法是归并排序。举个例子,的算法可以不是使用归并排序,但是该算法一定要是稳定的。这个类是的一部分。官方这个类只包含操作或返回集合的静态方法。具体来说是,第一步,先把集合转换为数组,第二步,调用。和没有什么区别,只是传参有点不同。

Arrays

1.作用
看类的名字,就知道是对数组(数据类型[])进行各种操作。例如,排序、查找、复制等。

排序的算法是归并排序。
查找的算法是二分查找。
复制是调用System.arraysCopy()。

2.官方API
public class Arrays
extends Object
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
这个类包含各种操作数组的方法,例如,排序、查找等。这个类也包含了一个静态工厂方法,允许数组被作为集合(list)查看。
如果指定的数组引用是null,那么方法会抛出NullPointerException。

The documentation for the methods contained in this class includes briefs description of the implementations. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort(Object[]) does not have to be a MergeSort, but it does have to be stable.)
方法的文档包含了实现的简单介绍,但是不属于规范的一部分。你可以用别的算法代替Arrays的算法,只要遵守规范即可。
举个例子,sort()的算法可以不是使用归并排序,但是该算法一定要是稳定的。

This class is a member of the Java Collections Framework.
这个类是Java Collections Framework的一部分。

Collections

1.作用
看类的名字,就知道是对集合类(Collection的子类)进行操作。例如,排序等。

排序的算法也是合并排序,调用的是Arrays.sort()。

2.官方API
public class Collections
extends Object
This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.
这个类只包含操作或返回集合的静态方法。它包含了多种算法...。

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.
如果方法的参数是null,那么会抛出NullPointerException 。

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)
同上。

The "destructive" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

This class is a member of the Java Collections Framework.

排序

1.Arrays
Arrays.sort()
2.Collections
Collections.sort()。
调用的也是Arrays.sort()。具体来说是,第一步,先把集合转换为数组,第二步,调用Arrays.sort()。

复制对象

1.Arrays
Arrays.copyOf()。
调用的是System.arrayCopy()。

Arrays.copyOf()和System.arrayCopy()没有什么区别,只是传参有点不同。

查找

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70680.html

相关文章

  • Java 并发设计模式

    摘要:并发设计模式一模式的使用表示线程本地存储模式。为不同的任务创建不同的线程池,这样能够有效的避免死锁问题。两阶段终止,即将线程的结束分为了两个阶段,第一个阶段是一个线程向另一个线程发送终止指令,第二个阶段是线程响应终止指令。 Java 并发设计模式 一、Thread Local Storage 模式 1. ThreadLocal 的使用 Thread Local Storage 表示线程...

    zero 评论0 收藏0
  • 小马哥Java项目实战训练营 极客大学

    摘要:百度网盘提取码一面试题熟练掌握是很关键的,大公司不仅仅要求你会使用几个,更多的是要你熟悉源码实现原理,甚至要你知道有哪些不足,怎么改进,还有一些有关的一些算法,设计模式等等。 ​​百度网盘​​提取码:u6C4 一、java面试题熟练掌握java是很关键的,大公司不仅仅要求你会使用几个api,更多的是要你熟悉源码实现原理,甚...

    不知名网友 评论0 收藏0
  • Java 中初始化 List 集合 6 种方式!

    摘要:是开发中经常会使用的集合,你们知道有哪些方式可以初始化一个吗这其中不缺乏一些坑,今天栈长我给大家一一普及一下。好了,今天栈长就给大家介绍到这里了,这种,你知道几种另外,也有类似的初始化的方法,大家有兴趣的可以试一下。 List 是 Java 开发中经常会使用的集合,你们知道有哪些方式可以初始化一个 List 吗?这其中不缺乏一些坑,今天栈长我给大家一一普及一下。 1、常规方式 List...

    Binguner 评论0 收藏0
  • Java程序员常犯10个错误

    摘要:原文出自本文总结了程序员常犯的个错误。可以看看为什么在中被设计成不可变父类和子类的构造函数以上这段代码出现编译错误,因为默认的父类构造函数未定义。如果程序员定义构造函数,编译器将不插入默认的无参数构造函数。 原文出自:http://www.programcreek.com/2014/05/top-10-mistakes-java-developers-make/ 本文总结了J...

    Andrman 评论0 收藏0
  • Java面试题

    摘要:近段时间在准备实习的面试,在网上看到一份面试题,就慢慢试着做,争取每天积累一点点。现在每天给自己在面试题编写的任务是题,有时候忙起来可能就没有时间写了,但是争取日更,即使当天没更也会在之后的更新补上。     近段时间在准备实习的面试,在网上看到一份面试题,就慢慢试着做,争取每天积累一点点。    暂时手头上的面试题只有一份,题量还是挺大的,有208题,所以可能讲的不是很详细,只是我自...

    OnlyMyRailgun 评论0 收藏0

发表评论

0条评论

mj

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<