1.多维数组:多维数组可以看成是数组的数组,二维数组的每一个元素都是一个一维数组。多维数组必须指定第一维的长度,其他维度可以指定也可以不指定。多维数组除第一维之外,其他维度可以有不同的任意长度。
public static void main(String[] args) { int[][] arr = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } }
2.数组与泛型:不能实例化具有参数类型的数组;可以参数化数组本身的类型;可以创建泛型数组的引用;可以把普通数组转型成泛型数组。
public static void main(String[] args) { Integer[] arr = {11, 22, 33, 44, 55}; f(arr); } public static <T> void f(T arr[]) { for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }
3.Arrays.fill():用同一个值填充数组的各个位置,对对象而言就是复制同一个引用进行填充。
public static void main(String[] args) { int[] i = new int[2]; String[] s = new String[2]; Arrays.fill(i, 123); System.out.println("i=" + Arrays.toString(i)); Arrays.fill(s, "hello"); System.out.println("s=" + Arrays.toString(s)); Arrays.fill(s, 1, 2, "world"); System.out.println("s=" + Arrays.toString(s)); }