Arrays實用功能(上)

前言    


在Java.util類庫中可以找到Arrays類,它有一套用於數組的static實用方法,其中有六個基本方法;
  • equals():用於比較兩個數組是否相等(deepEquals()用於多維數組);
  • fill():用於指定數組中的元素;
  • sort():用於對數組排序;
  • binarySearch():用於在已經排序的數組中查找元素(二分查找);
  • toString():產生數組的String表示;
  • hashCode():產生數組的散列碼。

所有這些方法對各種基本類型和Object類而重載過。此外,Aarrays.asList()接受任意的序列或數組作爲其參數,並將其轉變爲List容器。

複製數組


Java標準類庫提供有static方法System.arraycopy(),用它複製數組比用for循環複製要快很多。System.arraycopy()針對所有類型做了重載。下面的例子就是用來處理int數組的。
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]
其中System.arraycopy()參數說明如下:
* @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);
 這個例子說明基本類型數組與對象數組都可以複製。然而,如果複製對象數組,那麼只是複製了對象的引用----而不是對象本身的拷貝。這被稱爲”淺複製“。
System.arraycopy()不會執行自動包裝和自動拆包,兩個數組必須具有相同的確切類型。

數組的比較


Arrays類提供了重載後的equals()方法,用來比較整個數組。同樣,此方法針對所有基本類型與Object都做了重載。數組相等的條件是元素個數必須相等,並且對應位置的元素也相等。這可以通過對每一個元素使用equals0作比較來判斷。(對於基本類型,需要使用基本類型的包裝器類的equals()方法,例如,對於int類型使用Integer.equals()作比較)見下例:
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
最初,a1和a2完全相等,所以輸出爲true;然後改變其中一個元素,使得結果爲false。在最後一個例子中,s1的所有元素都指向同一個對象,而數組s2包含五個相互獨立的對象。然而,數組相等是基於內容的(Object.equals()比較),所以結果爲true。
Arrays.equals()源碼如下:
/**
* 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章