前言
- equals():用於比較兩個數組是否相等(deepEquals()用於多維數組);
- fill():用於指定數組中的元素;
- sort():用於對數組排序;
- binarySearch():用於在已經排序的數組中查找元素(二分查找);
- toString():產生數組的String表示;
- hashCode():產生數組的散列碼。
所有這些方法對各種基本類型和Object類而重載過。此外,Aarrays.asList()接受任意的序列或數組作爲其參數,並將其轉變爲List容器。
複製數組
import java.util.Arrays;
public class CopyingArrays {
public static void main(String[] args) {
int[] i = new int[7];
int[] j = new int[10];
Arrays.fill(i, 47); // 對數組賦值
Arrays.fill(j, 99);
System.out.println("i = " + Arrays.toString(i)); // 打印數組
System.out.println("j = " + Arrays.toString(j));
System.arraycopy(i, 0, j, 0, i.length); //複製數組
System.out.println("j = " + Arrays.toString(j));
int[] k = new int[5];
Arrays.fill(k, 103);
System.arraycopy(i, 0, k, 0, k.length);
System.out.println("k = " + Arrays.toString(k));
Arrays.fill(k, 103);
System.arraycopy(k, 0, i, 0, k.length);
System.out.println("i = " + Arrays.toString(i));
// Object
Integer[] u = new Integer[10];
Integer[] v = new Integer[5];
Arrays.fill(u, new Integer(47));
Arrays.fill(v, new Integer(99));
System.out.println("u = " + Arrays.toString(u));
System.out.println("v = " + Arrays.toString(v));
System.arraycopy(v, 0, u, u.length / 2, v.length);
System.out.println("u = " + Arrays.toString(u));
}
}
i = [47, 47, 47, 47, 47, 47, 47]
j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
k = [47, 47, 47, 47, 47]
i = [103, 103, 103, 103, 103, 47, 47]
u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]
v = [99, 99, 99, 99, 99]
u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]
* @param src the source array. // 源數組
* @param srcPos starting position in the source array. // 從源數組中的什麼位置開始複製的偏移量
* @param dest the destination array.
// 目標數組 * @param destPos starting position in the destination data.
// 從目標數組中的什麼位置開始複製的偏移量 * @param length the number of array elements to be copied. // 需要複製的元素個數
* @exception IndexOutOfBoundsException if copying would cause // 如果複製會導致對數組範圍以外的數據的訪問
* access of data outside array bounds.
* @exception ArrayStoreException if an element in the <code>src</code> //如果因爲類型不匹配而使得無法將 src 數組中的元素存儲 到 dest 數組中。
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or // 如果 src 或 dest 爲 null
* <code>dest</code> is <code>null</code>.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
數組的比較
import java.util.Arrays;
public class ComparingArrays {
public static void main(String[] args) {
int[] a1 = new int[10];
int[] a2 = new int[10];
Arrays.fill(a1, 47);
Arrays.fill(a2, 47);
System.out.println(Arrays.equals(a1, a2)); // 比較基本類型數組
a2[3] = 11;
System.out.println(Arrays.equals(a1, a2));
String[] s1 = new String[4];
Arrays.fill(s1, "hi");
String[] s2 = {new String("hi"),new String("hi"),new String("hi"),new String("hi")};
System.out.println(Arrays.equals(s1, s2)); // 比較對象數組
}
}
true
false
true
/**
* Returns <tt>true</tt> if the two specified arrays of ints are
* <i>equal</i> to one another. Two arrays are considered equal if both
* arrays contain the same number of elements, and all corresponding pairs
* of elements in the two arrays are equal. In other words, two arrays
* are equal if they contain the same elements in the same order. Also,
* two array references are considered equal if both are <tt>null</tt>.<p>
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return <tt>true</tt> if the two arrays are equal
*/
public static boolean equals(int[] a, int[] a2) {
if (a==a2) // 對象地址相等,返回true
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;
}