博客新址: http://blog.xuezhisd.top
邮箱:[email protected]
String[] strArray = {"zhang", "xue", "zhi"};
List list = Arrays.asList(strArray);
// 打印元素
for (int i=0; i
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);
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();
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();
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
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));
// 二维数组有区别
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)); // 一般
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 + " ");
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());
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();
int[] a = {6,5,4,3,2,1};
// 遍历数组 for
for (int i=0; i
int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
// 遍历二维数组 for
for (int i=0; i
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 + " ");
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));
int[] a = {1,2,3,4,5,6};
System.out.println(a.getClass());
int[] a = {1,2,3,4,5,6};
// 两种方法的结果不同。
System.out.println(a.hashCode());
System.out.println(Arrays.hashCode(a)); // Arrays.hashCode()
int[] a = {1,2,3,4,5,6};
System.out.println(a.toString()); // 地址
System.out.println(Arrays.toString(a)); //字符串
int[] a = {1,2,3,4,5,6};
int len = a.length;
System.out.println(len);