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);
*/
}
}