Arrays是一個java.util包下的一個數組工具類,提供了大量的static方法用來對數組進行操作,方便程序員開發中使用。具體方法如下:
一、搜索方法:
1.1、int binarySearch(type[] a ,type key)
作用:使用二分法,從一個數組中查找key的下標值,如果存在返回下標,如果不存在返回值為負數。
在源碼中,對這個方法進行是重載,參數的類型在變化,從8個基本數據類型 ,到Object以及自定義的類型都可以使用該方法進行查找元素。
1.2、int binarySearch(type[] a ,type key,int fromIndex,int toIndex)
作用:重載上一個方法,增加了開始下標和結束下標,也就是規定了查找的范圍。
1.3、源碼:
PRivate static int binarySearch0(long[] a, int fromIndex, int toIndex,long key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
long 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.
}
解讀:獲取任意類型的數組,一個對應類型的元素key,還有int類型的開始和結束下標,為了公用一個算法方法,我們先給定兩個局部變量,如果沒有指定開始和結束下標那么指定從0開始到數組a的長度-1結束。然后循環,循環判定條件是最小范圍值小于等于最大范圍值。然后中間值和key的比較,確定范圍,最后確定key所在位置的index,直接返回。如果沒有該值,直接返回負數。
二、復制方法:
2.1、type[] copyOf(type[] original,int length)
作用:復制original數組到一個新數組,length為新數組的長度,如果length的值大于original的長度,那么新數組的其他值補0,0.0,false,null等
2.2、type[] copyOfRange(type[] original,int fromIndex,int toIndex)
作用:該方法是上面方法的重載,規定了開始復制的下標和結束復制的下標,也就是復制規定位置的數組元素到新數組中。
2.3、源碼:
public static byte[] copyOf(byte[] original, int newLength) {
byte[] copy = new byte[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
Java中這個方法是調用了System類中的arraycopy方法實現的,但是arraycopy有一個修飾符native是咋回事了?如果方法用native關鍵字修飾,說明該方法有實現,但不是使用java代碼實現的,它的實現是在一個DLL文件中,可能使用C語言等其他語言實現,方便了java和硬件的交互,缺點是增加開銷。Native方法也被稱為本地方法。
public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
解讀:先計算出新數組的長度,使用結束下標-開始下標,如果小于0,說明結束下標小于開始下標,這樣不成立,所以手動拋出異常。然后調用System類的arraycopy方法,執行。
2.4、啟發:寫代碼要有一定的嚴謹性,有開始結束的位置下標,一定要比較兩個的大小,拋出異常。
我們也可以直接調用System類的arraycopy方法,對數組直接進行賦值
三、判斷方法:
3.1、boolean equals(long[] a1,long[] a2)
作用:判斷兩個數組的長度和內容是否相同(數組元素必須一一對應并相同)
3.2、源碼:
public static boolean equals(long[] a, long[] 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;
}
四、賦值方法:
4.1、void fill(type[] a,type val)
作用:將數組中的所有元素都賦值為val
4.2、void fill(type[] a,int fromIndex,int toIndex,type val)
作用:同上相同,指定了開始和結束的范圍
4.3、源碼:
public static void fill(byte[] a, byte val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
//范圍檢查方法
private static void rangeCheck(int length, int fromIndex, int toIndex) {
if (fromIndex > toIndex) {
throw new IllegalArgumentException(
"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
if (fromIndex < 0) {
throw new ArrayIndexOutOfBoundsException(fromIndex);
}
if (toIndex > length) {
throw new ArrayIndexOutOfBoundsException(toIndex);
}
}
新聞熱點
疑難解答