源码的类注释: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.
可见这就是一个处理数组的类,直接研究含有的方法。
asList
1 | /** |
binarySearch
源码方法注释:Searches the specified array of longs for the specified value using the binary search algorithm. The array must be sorted
就是 采用二进制搜索算法,而且其数组必须是要经过排序的(后面的binarySearch0的使用基础必须是已经排序好的数组),排序刚好可以使用它的sort方法。
1 | /** |
copyOf
1 |
|
1 | /** |
可以看到最终实现的都是System.arraycopy()
deepEquals
1 | /** |
###
源码方法注释:Returns a hash code based on the “deep contents” of the specified array.
1 | /** |
deepToString
就是把数组转换成“[xx,yy,zz…]”,这样的字符串,可转换多层次嵌套的数组。而toString方法就只能转换一层数组。
equals
1 | /** |
fill
1 | /** |
1 | /** |
hashCode
1 | /** |
legacyMergeSort
源码方法注释有这玩意:/* To be removed in a future release. /,那就不看他了
parallelPrefix、parallelSetAll
太少用了,忽略不看。
parallelSort
1 | /** |
setAll
不常用,忽略
sort
该方法是用于数组排序,在 Arrays 类中有该方法的一系列重载方法,能对7种基本数据类型,包括 byte,char,double,float,int,long,short 等都能进行排序,还有 Object 类型(实现了Comparable接口),以及比较器 Comparator 。这里我们以 int[ ] 为例看看。
1 | /** |
在 Arrays.sort 方法内部调用 DualPivotQuicksort.sort 方法,这个方法的源码很长,分别对于数组的长度进行了各种算法的划分,包括快速排序,插入排序,冒泡排序都有使用。特意翻译了DualPivotQuicksort这个类,有兴趣可以往下看(会逐渐翻译完毕)。
1 | final class DualPivotQuicksort { |