Java中的Arrays,這一篇就夠了
哈嘍,大家好,我是了不起。
JDK中提供了一個專門用于操作數組的工具類,即Arrays類,位于java.util 包中。
常用方法
返回類型 | 方法 | 說明 |
String | toString(array) | 將數組array轉換成字符串 |
void | sort(array) | 對數組進行升序排列。注意:排序算法是由Vladimir Yaroslavskiy,Jon Bentley和Joshua Bloch提供的雙軸快速排序。 |
void | fill(arr,val) | 將數組arr全部元素賦值為val |
boolean | equals(arr1,arr2) | 判斷兩個數組是否相等 |
與arr類型相同 | copyOf(arr,length) | 將數組arr復制成一個長度為length的新數組 |
int | binarySearch(arr, val) | 查詢元素val在arr中的下標值 |
示例代碼
public class Test {
public static void main(String[] args) {
int a[]={12,20,13,42,72,26,35,10,46,26,53};
int b[]={3,5,7,8,54,23,9};
int c[]={3,5,7,8,54,23,9};
String str=Arrays.toString(a); //將特定數組轉換成字符串
System.out.println("字符串:"+str);
Arrays.sort(a); //對數組array的元素進行升序排列
System.out.println("排序后:"+Arrays.toString(a));
Arrays.fill(a,10); //所以元素都賦值成特定值
System.out.println("賦值后:"+Arrays.toString(a));
boolean boo=Arrays.equals(a,b); //判斷兩個數組是否相等(對應位置上的元素是否相等)
boolean boo2=Arrays.equals(b, c);
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("ab相等?"+boo);
System.out.println("bc相等?"+boo2);
int d[]=Arrays.copyOf(b,b.length); //把數組復制成特定長度的數組,與直接賦值(引用傳遞)不同
System.out.println("d:"+Arrays.toString(d));
System.out.println("d:"+d);
System.out.println("b:"+b);
int i=Arrays.binarySearch(b, 5); //查詢特定因素在數組中的下標
System.out.println("下標是:"+i);
}
}
運行結果:
字符串:[12,20,13,42,72,26,35,10,46,26,53]排序后:[10,12,13,20,26,26,35,42,46,53,72]賦值后:「10,10,10,10,10,10,10,10,10,10,10]
a:[I@1606bf5b:[I@14fcc96
c:[I@bcbc
ab相等?false
bc相等?true
d:[3,5,7,8,54,23,9]d:[I@671416b:[I@14fcc96
下標是:1
源碼解析
將數組array轉換成字符串
public static String toString(int[] a)
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr));
public static String toString(int[] a) {
//a -- arr -- { 24, 69, 80, 57, 13 }
if (a == null)
return "null"; //說明數組對象不存在
int iMax = a.length - 1; //iMax=4;
if (iMax == -1)
return "[]"; //說明數組存在,但是沒有元素。
StringBuilder b = new StringBuilder();
b.append('['); //"["
for (int i = 0; ; i++) {
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax)
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
二分查找
public static int binarySearch(int[] a,int key)
int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
public static int binarySearch(int[] a, int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//key -- 577
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//fromIndex -- 0
//toIndex -- 5
//key -- 577
int low = fromIndex; //low=0
int high = toIndex - 1; //high=4
while (low <= high) {
int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
int midVal = a[mid]; //midVal=57,midVal=69,midVal=80
if (midVal < key)
low = mid + 1; //low=3,low=4,low=5
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
復制
public static int[] copyOf(int[] original, int newLength)
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
可以看到,最終調用的是System.arraycopy()方法,由虛擬機實現,效率自然比用java一個個復制高。
深復制與淺復制
- 當數組為一維數組,且元素為基本類型或String類型時,屬于深復制,即原數組與新數組的元素不會相互影響。
- 當數組為多維數組,或一維數組中的元素為引用類型時,屬于淺復制,原數組與新數組的元素引用指向同一個對象。這里說的影響,是兩個數組復制后對應的元素。String的特殊是因為它的不可變性。
一維數組,元素為基本類型
public class SystemArrayCopy {
public static void main(String[] args) {
String str1 = "aa";
String str2 = "bb";
String str3 = "cc";
String str4 = "dd";
String[] src = {str1, str2, str3, str4};
String[] dest = new String[4];
System.arraycopy(src, 0, dest, 0, 4);
System.out.println("改變前");
print("src = ", src);
print("dest = ", dest);
src[0] = "abcd";
System.out.println("改變后");
print("src = ", src);
print("dest = ", dest);
}
private static void print(String string, String[] arr) {
System.out.print(string);
for (String str : arr) {
System.out.print(str + " ");
}
System.out.println();
}
}
/*
改變前
src = aa bb cc dd
dest = aa bb cc dd
改變后
src = abcd bb cc dd
dest = aa bb cc dd
*/
可以看到,源數組第0個元素改變,并不會影響到目標數組。
多維數組
public class SystemArrayCopy {
public static void main(String[] args) {
int[] arr1 = {1, 2};
int[] arr2 = {3, 4};
int[] arr3 = {5, 6};
int[] arr4 = {7, 8};
int[][] src = new int[][]{arr1, arr2, arr3, arr4};
int[][] dest = new int[4][];
System.arraycopy(src, 0, dest, 0, 4);
System.out.println("改變前");
print("src = ", src);
print("dest = ", dest);
src[0][0] = 11111;
System.out.println("改變后");
print("src = ", src);
print("dest = ", dest);
}
// 簡單輸出二維int數組的方法
private static void print(String string, int[][] arr) {
System.out.print(string);
for (int[] a : arr) {
for (int i : a) {
System.out.print(i + " ");
}
System.out.print(",");
}
System.out.println();
}
}
/*
改變前
src = 1 2 ,3 4 ,5 6 ,7 8 ,
dest = 1 2 ,3 4 ,5 6 ,7 8 ,
改變后
src = 11111 2 ,3 4 ,5 6 ,7 8 ,
dest = 11111 2 ,3 4 ,5 6 ,7 8 ,
*/
源數組改變后,目標數組也跟改變了,這就是淺復制
數組拷貝的4種方法
1. for循環
使用for循環自己實現數組的復制
2. clone
克隆方法我們在數組中是找不到的,它是object的方法,我們先看看源碼
protected native Object clone() throws CloneNotSupportedException;
看到了修飾符native,說明是由c或者c++實現的,它的優點是速度快,它返回了object對象,所以使用的時候需要用對象接收返回值。
3. System.arraycopy()
通過上述源碼我們看到也是native修飾的,所以底層也是用c或者c++實現的,但是可以看到沒有返回值,clone()還需要對返回值進行類型轉換,所以它的速度是要比clone()要快的,這也是牛客網的一道題,問的就是四種拷貝哪種是最快的,答案肯定是System.arraycopy()。
4. Arrays.copyof()
在方法內部調用了System.arraycopy(),相當于換了名字。
結語
本次我們介紹了Java中的Arrays的常用方法,Arrays如何去操作數組、拷貝數組和打印數組的方法。