目录
简介
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。静态方法是属于类的,不是属于类的对象。所以可以直接使用类名加方法名进行调用。Arrays作为一个工具类,能很好的操作数组。
fill方法
用途:fill方法主要用来填充数组,以简单的int数组为栗。
源码:
/**
* Assigns the specified int value to each element of the specified array
* of ints.
*
* @param a the array to be filled
* @param val the value to be stored in all elements of the array
*/
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
测试:
public static void testFill(){
int[] arr = new int[4];
Arrays.fill(arr, 1);
System.out.println(Arrays.toString(arr));
}
结果:
asList方法
用途:数组转集合,Returns a fixed-size list backed by the specified array。
源码:
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
测试:
public static void testAsList(){
String[] arr = {"Eddie","Daniel","Richard"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
System.out.println(list);
}
结果:
sort方法
用途:Sorts the specified array into ascending numerical order(将指定的数组排序为升序)
源码:
public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
public static void sort(int[] a, int fromIndex, int toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
}
测试:
public static void testSort(){
int[] arr = {2,6,4,8,9,5};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
结果:
equals方法
用途:判断两个数组是否相等,如果两个数组有相同顺序的相同元素,它们就相等。(In other words, two arrays are equal if they contain the same elements in the same order.)
源码:
public static boolean equals(int[] a, int[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++)
if (a[i] != a2[i])
return false;
return true;
}
测试:
public static void testEquals(){
int[] arr = {1,2,3,4,5};
int[] arr2 = {1,2,3,4,5};
int[] arr3 = {5,4,3,2,1};
System.out.println("arr==arr2:"+Arrays.equals(arr, arr2));
System.out.println("arr==arr3:"+Arrays.equals(arr, arr3));
}
结果:
binarySearch方法
用途:Searches the specified array of xx for the specified value using the binary search algorithm. 使用二分查找法在指定数组中搜索指定值。
前提条件:要求数组有序,因为二分查找每次去除中间值与key(搜索值)比较,然后逐渐缩小范围进行查找。
源码:
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
测试:
public static void testBinarySearch(){
int[] arr = {1,3,6,8,9};
int index = Arrays.binarySearch(arr, 3);
System.out.println(index);
}
结果:
copyOf方法
用途:Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
复制指定数组指定长度。
源码:
@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
测试:
public static void testCopyOf(){
int[] arr = {1,3,5,7,9};
int[] arr2 = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.toString(arr2));
}
结果:
toString方法
用途:返回指定数组内容的字符串表示。
源码:
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
测试:
public static void testToString(){
int[] arr = {2,4,6,8,10};
System.out.println(Arrays.toString(arr));
}
结果: