本帖最后由 何万县 于 2012-3-31 19:17 编辑
在java中,不能通过直接通过T[] tarr=new T[10]的方式来创建数组,最简单的方式便是通过Array.newInstance(Class<t>type,int size)的方式来创建数组
例如下面的程序。
public class ArrayMaker<T> {
private Class<T> type;
public ArrayMaker(Class<T> type) {
this.type = type; }
@SuppressWarnings("unchecked")
T[] createArray(int size) {
return (T[]) Array.newInstance(type, size);
}
List<T> createList() {
return new ArrayList<T>();
} /** * @param args */
public static void main(String[] args) {
/* * Even though kind is stored as Class<T> , erasure means that it is actually just being stored as a Class, with
* no parameter. So, when you do some thing with it, as in creating an array, Array.newInstance( ) doesn’t
* actually have the type information that’s implied in kind; so it cannot produce the specific result, wh ich
* must therefore be cast, which produces a warning that you cannot satisfy.
*/
ArrayMaker<Type> am2 = new ArrayMaker<Type>(Type.class);
System.out.println(Arrays.asList(am2.createArray(10))); System.out.println(Arrays.asList(am2.createList()));
}
}
class Type {
@Override
public String toString() {
return "type";
}
}
|