import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Ans {
public static void main(String[] args) {
A []a1 = new A[4];
//! a1[0] = new B(); 編譯時報錯
ArrayList<A> a2 = new ArrayList<>();
//! a2.add(new B()); 帶有標籤的情況會在編譯時報錯
ArrayList a3 = new ArrayList();
a3.add(new B());
a3.add(new A());
Object b = a3.get(0); //這種情況只能得到Object,如果想得到具體類型,需要轉型,錯誤的轉型會導致運行時報錯,所以最好不要使用無標籤的泛型
//對於java SE5 deepToString的使用
Integer [][]x = {{1,2,3},{4,5,6}};
System.out.println(Arrays.deepToString(x));
//對粗糙數組的使用
Random rand = new Random(47);
Integer [][][]y = new Integer[rand.nextInt(5)][][];
for (int i = 0;i<y.length;++i) {
y[i] = new Integer[rand.nextInt(5)][];
for (int j = 0;j<y[i].length;++j)
y[i][j] = new Integer[rand.nextInt(5)];
}
System.out.println(Arrays.deepToString(y));
//對泛型與數組間的一些研究
//! List<Integer>[] ls = new List<Integer>[10]; 你不能直接實例化一個泛型數組,因爲泛型具有擦除,而數組需要具體的類檢查機制
List<Integer>[] ls = (List<Integer> []) new List[10]; //但你可以這麼做
//! ls[0] = new ArrayList<String>(); //而且會提供檢查機制
//! ls = (List<Object> []) ls; 這樣明顯也是不可以的,根據泛型機制List<Object>與List<Integer>沒有關係
Object []ls2 = ls; //但這麼做的可行性又給這種機制帶來了潛在的威脅
ls2[1] = new A(); //可以放入一個不相關的東西了
}
}
class A {}
class B {}