Java數組經常使用API

java.util.Arrays
Arrays.asList()
數組轉換成列表
String[] strArray = {"zhang", "xue", "zhi"};
List<String> list = Arrays.asList(strArray);
// 打印元素
for (int i=0; i<list.size(); i++) {
    System.out.print(list.get(i) + " ");
}
1
2
3
4
5
6
Arrays.binarySearch()
二分查找
查找前,必定要排序。
若是查找元素不存在,返回(-(insertion point) - 1)。
天然數表示查到,負數表示沒有查找。
int[] a = {3,5,9,7,2};
Arrays.sort(a); // 排序
// 打印數組
for (int item : a) 
    System.out.print(item + " ");
System.out.println();
// 二分查找
int ind1 = Arrays.binarySearch(a, 2);
int ind2 = Arrays.binarySearch(a, 4);
int ind3 = Arrays.binarySearch(a, 1, 3, 5);
System.out.println("2的查找位置:" + ind1);
System.out.println("4的查找位置:" + ind2);
System.out.println("5的查找位置:" + ind3);

// 字符串
String[] strArray = {"aa", "bc", "ab", "cd"};
Arrays.sort(strArray);
int ind4 = Arrays.binarySearch(strArray, "bc");
System.out.println("'bc'的查找位置是:" + ind4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Arrays.copyOf()
複製長度大於原數組長度時,後面補零。
int[] a = {3,5,9,7,2};
int[] newa = Arrays.copyOf(a, 2);
int[] newa2 = Arrays.copyOf(a, 7);  //複製長度大於原數組的長度

for (int item : newa) 
    System.out.print(item + " ");
System.out.println();

for (int item : newa2) 
    System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.copyOfRange()
複製長度大於原數組長度時,後面補零。
Java中區間通常都是左閉右開[a,b),即包括左邊,不包括右邊。
int[] a = {3,5,9,7,2};
int[] newa = Arrays.copyOfRange(a, 1, 3);
int[] newa2 = Arrays.copyOfRange(a, 1, 8);  //複製長度大於原數組的長度

for (int item : newa) 
    System.out.print(item + " ");
System.out.println();

for (int item : newa2) 
    System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.deepEquals()
比較數組元素是否深層相等。
一維數組無區別,高維數組有區別。
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
String[][] ticTacToe2 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.equals(ticTacToe1, ticTacToe2));// false
System.out.println(Arrays.deepEquals(ticTacToe1, ticTacToe2));// true

String[] ticTacToe3 = { " O ", " O ", " X " };
String[] ticTacToe4 = { " O ", " O ", " X " };  
System.out.println(Arrays.equals(ticTacToe3, ticTacToe4));  // true
System.out.println(Arrays.deepEquals(ticTacToe3, ticTacToe4));  // true
1
2
3
4
5
6
7
8
9
Arrays.deepHashCode()
深層相等的兩個數組的深層哈希編碼也相等。
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
String[][] ticTacToe2 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.equals(ticTacToe1, ticTacToe2));// false
System.out.println(Arrays.deepEquals(ticTacToe1, ticTacToe2));// true
// ticTacToe1和ticTacToe1深層相等,深層哈希編碼也相等。
System.out.println(Arrays.deepHashCode(ticTacToe1));
System.out.println(Arrays.deepHashCode(ticTacToe2));
1
2
3
4
5
6
7
Arrays.deepToString()
// 二維數組有區別
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.deepToString(ticTacToe1));    // 深層變換成字符串
System.out.println(Arrays.toString(ticTacToe1));        // 通常

// 一維數組無區別
String[] ticTacToe3 = { " O ", " O ", " X " };
System.out.println(Arrays.deepToString(ticTacToe3));    // 深層變換成字符串
System.out.println(Arrays.toString(ticTacToe3));        // 通常
1
2
3
4
5
6
7
8
9
Arrays.equals()
參考Arrays.deepArrays()
Arrays.fill()
填充數組元素
int[] a = {1,2,3,4,5,6};

Arrays.fill(a, 8);//所有填充
for (int item : a) 
    System.out.print(item + " ");
System.out.println();

Arrays.fill(a, 1, 3, 0);//指定範圍,替換
for (int item : a) 
    System.out.print(item + " ");
1
2
3
4
5
6
7
8
9
10
Arrays.hashCode()
若是兩個數組相等,哈希編碼也相等。
int[] a = {1,2,3,4,5,6};
int[] b = {1,2,3,4,5,6};
String[] c = {"a", "b", "c"};
String[] d = {"a", "b", "c"};

System.out.println("a==b:" + Arrays.equals(a, b));
System.out.println("a和b的哈希碼分別爲:" + Arrays.hashCode(a) + "\t" + Arrays.hashCode(b));
// 字符串
System.out.println("c==d:" + Arrays.equals(c, d));
System.out.println("c和d的哈希碼分別爲:" + Arrays.hashCode(c) + "\t" + Arrays.hashCode(d));

System.out.println("c==d:" + c.equals(d));
System.out.println("c和d的哈希碼分別爲:" + c.hashCode() + "\t" + d.hashCode());
1
2
3
4
5
6
7
8
9
10
11
12
13
Arrays.sort()
int[] a = {6,5,4,3,2,1};
// 指定範圍排序
Arrays.sort(a, 1, 4);
for (int item : a) 
    System.out.print(item + " ");
System.out.println();
// 所有元素排序
Arrays.sort(a);
for (int item : a) 
    System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.toString()
參考 Arrays.deepToString()
遍歷數組
遍歷一維數組
int[] a = {6,5,4,3,2,1};
// 遍歷數組 for
for (int i=0; i<a.length; i++)
    if (i == a.length-1)
        System.out.print(a[i]);
    else
        System.out.print(a[i] + ", ");
System.out.println();

// 遍歷數組  - foreach
for (int item : a)
    if (item == a[a.length-1])
        System.out.print(item);
    else 
        System.out.print(item + ", ");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
遍歷二維數組
int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
// 遍歷二維數組 for
for (int i=0; i<a.length; i++) {
    for (int j=0; j<a[i].length; j++)
        if (j == a[i].length-1)
            System.out.print(a[i][j]);
        else
            System.out.print(a[i][j] + ", ");
    System.out.println();
}
1
2
3
4
5
6
7
8
9
10
數組對象的方法
數組從java.lang.Object繼承的方法:clone, equals, finalize, getClass, hashCode, notify, toString, wait
arr.clone()
經過克隆生成另外一個數組
int[] a = {1,2,3,4,5,6};
int[] b = a.clone();
Arrays.fill(a, 1, 4, 0); // 改變a,看是否對b有影響
//打印b
for (int item : b) 
    System.out.print(item + " ");
1
2
3
4
5
6
arr.equals()
arr.equals()和Arrays.equals()不一樣
int[] a = {1,2,3,4,5,6};
int[] b = a.clone();
int[] c = a;
// 判等
System.out.println(Arrays.equals(a, b)); // 比較內容
System.out.println(a.equals(b));    //比較地址
System.out.println(a.equals(c));
1
2
3
4
5
6
7
arr.getClass()
int[] a = {1,2,3,4,5,6};

System.out.println(a.getClass());
1
2
3
arr.hashCode()
int[] a = {1,2,3,4,5,6};

// 兩種方法的結果不一樣。
System.out.println(a.hashCode()); 
System.out.println(Arrays.hashCode(a)); // Arrays.hashCode()
1
2
3
4
5
arr.toString()
int[] a = {1,2,3,4,5,6};

System.out.println(a.toString());   // 地址
System.out.println(Arrays.toString(a)); //字符串
1
2
3
4
數組對象的屬性
arr.length
int[] a = {1,2,3,4,5,6};

int len = a.length;
System.out.println(len);

轉自:https://blog.csdn.net/xuezhisdc/article/details/52346800java

參考:https://blog.csdn.net/qq_19558705/article/details/50436583數組

相關文章
相關標籤/搜索