A[] a = new A[4]; // 基本類型被被初始化為0,對象類型會被初始化為null
A[] b = {new A(), new A(), new A()};
A c = new A[]{new A(), new A()};
多維數組
int[][] a = {{1, 2, 3}, {4, 5, 6}};
int[][] b = new int[2][2];
int[][][] c = new int[2][][];
for (int i = 0; i < c.length; i++) {
c[i] = new int[2][];
for (int j = 0; j < c[i].length; j++) {
c[i][j] = new int[2];
}
}
數組與泛型
創建泛型實例數組
// 擦除使數組不能知道確切類型
List<Integer>[] a1 = new List<Integer>[10];
List<Integer>[] a2;
List[] a3 = new List[10];
a2 = (List<Integer>[]) a3; // a2可以繞過檢查,創建泛型實例數組