package com.xujin; import java.util.Arrays; public class Test { public static void main(String[] args){ int[] a = {2, 3, 5, 7}; int[] b = a;//此时a与b引用同一个数组 System.out.println(Arrays.toString(a));//[2, 3, 0, 7] System.out.println(Arrays.toString(b));//[2, 3, 0, 7] b = new int[]{12, 13, 14};//用一个匿名数组初始化b System.out.println(Arrays.toString(a));//[2, 3, 0, 7] System.out.println(Arrays.toString(b));//[12, 13, 14] //public static int[] copyOf(int[] original, int newLength) int[] c = Arrays.copyOf(a, a.length); c[2] = 100; System.out.println(Arrays.toString(a));//[2, 3, 0, 7] System.out.println(Arrays.toString(c));//[2, 3, 100, 7] //public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) int[] d = {33, 44, 55, 66, 77, 88, 99}; System.arraycopy(d, 0, a, 1, 3); System.out.println(Arrays.toString(a));//[2, 33, 44, 55] System.out.println(Arrays.toString(d));//[33, 44, 55, 66, 77, 88, 99] //public static int[] copyOfRange(int[] original, int from, int to) int[] e = Arrays.copyOfRange(a, 1, 3); System.out.println(Arrays.toString(e));//[33, 44] } }
本测试在0~49共50个数里面任取6个数,按大小排序好了输出
package com.xujin; import java.util.Arrays; public class Test { static final int MAX = 50; static final int FIT = 6; public static void main(String[] args){ int[] total = new int[MAX]; int[] result = new int[FIT]; for(int i = 0; i < MAX; i++) total[i] = i; for(int i = 0; i < FIT; i++){ result[i] = (int) (Math.random() * MAX); } Arrays.sort(result); System.out.println(Arrays.toString(result)); } }
package com.xujin; import java.util.Arrays; public class Test { public static void main(String[] args){ int[] a = new int[100]; for(int i = 0; i < a.length; i++) a[i] = i; int[] b = Arrays.copyOfRange(a, 0, 10); System.out.println(Arrays.toString(b));//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] //public static int binarySearch(int[] a, int key) int r = Arrays.binarySearch(a, 23); System.out.println(r);//23 //public static void fill(int[] a, int val) Arrays.fill(b, 8); System.out.println(Arrays.toString(b));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8] //public static boolean equals(int[] a, int[] a2) int[] c = new int[]{8, 8, 8, 8, 8, 8, 8, 8, 8, 8}; if(Arrays.equals(b, c))//此时相等,返回一个true System.out.println("两个数组大小相等,并且下表相同的元素都对应相等"); } }
package com.xujin; import java.util.Arrays; public class Test { public static void main(String[] args){ int[][] matrix = { {1,2,3}, {3,2,1}, {4,5,6}, }; System.out.println(matrix[0][2]); //3 for(int i = 0; i < matrix.length; i++) System.out.println(Arrays.toString(matrix[i]));//[1, 2, 3] //[3, 2, 1] //[4, 5, 6] //快速打印一个二维数组的数据元素列表 System.out.println(Arrays.deepToString(matrix));//[[1, 2, 3], [3, 2, 1], [4, 5, 6]] //for each循环语句不能自动处理二维数组的每一个元素,他是按照一维数组处理的 for(int[] row : matrix) for(int value : row){ System.out.println(value * 10); } /* 10 20 30 30 20 10 40 50 60 */ } }
打印出如下数列:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
package com.xujin; public class Test { public static void main(String[] args){ int[][] test = new int[10][]; for(int n = 0; n < 10; n++){ test[n] = new int[n + 1]; } for(int i = 0; i < 10; i++) for(int j = 0; j < test[i].length; j++){ test[i][j] = j + 1; } for(int[] row : test){ for(int value : row) System.out.print(value + " "); System.out.println(); } } }