import java.util.ArrayList; import java.util.List; /** *泛型數組; *泛型數組不能new產生,單能夠是泛型數組引用;如List<String>[] ls; */ public class ArraysGeneric { public static <T> T[] get (int size) { /** * Error: reutrn new T[size]; */ return (T[])new Object[size]; } public static void main(String[] args) { List<String>[] ls; /**注意下面這兩段代碼的不一樣之處**/ //List<Object> list = new ArrayList<String>(); //error //Object [] objs = new String[3]; List [] la = new List[10]; ls = (List<String>[])la; ls[0] = new ArrayList<String>(); Object [] objects = ls; objects[1] = new ArrayList<String>(); List<String> [] ls2 = new List[2]; /**能夠強制轉換**/ List<String> [] ls3 = (List<String> [])new List[2]; /** * Error:Integer[] strs= get(4); */ } }