java-工具類Collections和Arrays的設計和區別

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。app

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()的算法能夠不是使用歸併排序,可是該算法必定要是穩定的。ide

This class is a member of the Java Collections Framework.
這個類是Java Collections Framework的一部分。ui

Collections

1.做用
看類的名字,就知道是對集合類(Collection的子類)進行操做。例如,排序等。this

排序的算法也是合併排序,調用的是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 。ip

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.)
同上。ci

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()沒有什麼區別,只是傳參有點不一樣。

查找

相關文章
相關標籤/搜索