Arrays是一個java.util包下的一個數(shù)組工具類,提供了大量的static方法用來對數(shù)組進(jìn)行操作,方便程序員開發(fā)中使用。具體方法如下:
一、搜索方法:
1.1、int binarySearch(type[] a ,type key)
作用:使用二分法,從一個數(shù)組中查找key的下標(biāo)值,如果存在返回下標(biāo),如果不存在返回值為負(fù)數(shù)。
在源碼中,對這個方法進(jìn)行是重載,參數(shù)的類型在變化,從8個基本數(shù)據(jù)類型 ,到Object以及自定義的類型都可以使用該方法進(jìn)行查找元素。
1.2、int binarySearch(type[] a ,type key,int fromIndex,int toIndex)
作用:重載上一個方法,增加了開始下標(biāo)和結(jié)束下標(biāo),也就是規(guī)定了查找的范圍。
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.
}
解讀:獲取任意類型的數(shù)組,一個對應(yīng)類型的元素key,還有int類型的開始和結(jié)束下標(biāo),為了公用一個算法方法,我們先給定兩個局部變量,如果沒有指定開始和結(jié)束下標(biāo)那么指定從0開始到數(shù)組a的長度-1結(jié)束。然后循環(huán),循環(huán)判定條件是最小范圍值小于等于最大范圍值。然后中間值和key的比較,確定范圍,最后確定key所在位置的index,直接返回。如果沒有該值,直接返回負(fù)數(shù)。
二、復(fù)制方法:
2.1、type[] copyOf(type[] original,int length)
作用:復(fù)制original數(shù)組到一個新數(shù)組,length為新數(shù)組的長度,如果length的值大于original的長度,那么新數(shù)組的其他值補(bǔ)0,0.0,false,null等
2.2、type[] copyOfRange(type[] original,int fromIndex,int toIndex)
作用:該方法是上面方法的重載,規(guī)定了開始復(fù)制的下標(biāo)和結(jié)束復(fù)制的下標(biāo),也就是復(fù)制規(guī)定位置的數(shù)組元素到新數(shù)組中。
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中這個方法是調(diào)用了System類中的arraycopy方法實現(xiàn)的,但是arraycopy有一個修飾符native是咋回事了?如果方法用native關(guān)鍵字修飾,說明該方法有實現(xiàn),但不是使用java代碼實現(xiàn)的,它的實現(xiàn)是在一個DLL文件中,可能使用C語言等其他語言實現(xiàn),方便了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;
}
解讀:先計算出新數(shù)組的長度,使用結(jié)束下標(biāo)-開始下標(biāo),如果小于0,說明結(jié)束下標(biāo)小于開始下標(biāo),這樣不成立,所以手動拋出異常。然后調(diào)用System類的arraycopy方法,執(zhí)行。
2.4、啟發(fā):寫代碼要有一定的嚴(yán)謹(jǐn)性,有開始結(jié)束的位置下標(biāo),一定要比較兩個的大小,拋出異常。
我們也可以直接調(diào)用System類的arraycopy方法,對數(shù)組直接進(jìn)行賦值
三、判斷方法:
3.1、boolean equals(long[] a1,long[] a2)
作用:判斷兩個數(shù)組的長度和內(nèi)容是否相同(數(shù)組元素必須一一對應(yīng)并相同)
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)
作用:將數(shù)組中的所有元素都賦值為val
4.2、void fill(type[] a,int fromIndex,int toIndex,type val)
作用:同上相同,指定了開始和結(jié)束的范圍
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);
}
}
新聞熱點
疑難解答