java 如何创建泛型数组_Java中泛型数组创建总结

在java中,可以声明一个泛型数组,不能通过直接通过T[] tarr=new T[10]的方式来创建数组,最简单的方式便是通过Array.newInstance(Classtype,int size)的方式来创建数组例如下面的程序。

public class ArrayMaker {

private Class type;

public ArrayMaker(Class type) {

this.type = type;

}

@SuppressWarnings("unchecked")

T[] createArray(int size) {

return (T[]) Array.newInstance(type, size);

}

List createList() {

return new ArrayList();

}

/***@paramargs*/

public static void main(String[] args) {

/** Even though kind is stored as Class , 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, which

* must therefore be cast, which produces a warning that you cannot satisfy.*/

ArrayMaker am2 = new ArrayMaker(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";

}

}

程序一:这个程序主要说明了,在使用泛型数组中容易出现的问题,由于书中对于程序的说明比较详细,所以只对程序做引用。

class Generic {

}

public class ArrayofGeneric {

public static void main(String[] args) {

Generic[] genArr;

genArr = (Generic[]) new Generic[2];

System.out.println(genArr);

}

}

程序二:这个程序主要是说明在程序的执行过程中,泛型数组的类型信息会被擦除,且在运行的过程中数组的类型有且仅有Object[],如果我们强制转换成T[]类型的话,虽然在编译的时候不会有异常产生,但是运行时会有ClassCastException抛出。

public class ArrayOfGeneric2{publicT[] ts;public ArrayOfGeneric2(intsize) {

ts= (T[]) newObject[size];

}public T get(intindex) {returnts[index];

}publicT[] rep() {returnts;

}public void set(intindex, T t) {

ts[index]=t;

}public static void main(String[] args) {

ArrayOfGeneric2 aog2 = new ArrayOfGeneric2(10);

Object[] objs=aog2.rep();

System.out.println(objs);/*will throw ClassCastException*/ // String[] strs = aog2.rep(); //System.out.println(strs);

}

}

程序三:主要说明在对象中通过用Object[]来保存数据,则生成对象是,可以对其持有的对象在T和object之间进行转换,但是当设计到数组的转换时,还是会报ClassCastException

public class ArrayOfGeneric3{

Object[] ts;public ArrayOfGeneric3(intsize) {

ts= newObject[size];

}public T get(intindex) {return(T) ts[index];

}publicT[] rep() {return(T[]) ts;

}public void set(intindex, T t) {

ts[index]=t;

}public static void main(String[] args) {

ArrayOfGeneric3 aog2 = new ArrayOfGeneric3(10);

Object[] objs=aog2.rep();for (int i = 0; i < 10; i++) {

aog2.set(i, i);

System.out.println(aog2.get(i));

}

Integer[] strs=aog2.rep();

System.out.println(strs);

}

}

程序四:是对泛型数组相对而言比较完美的解决方案

public class ArrayOfGeneric4{

T[] ts;public ArrayOfGeneric4(Class type, intsize) {/*to solution array of generic key code!*/ts=(T[]) Array.newInstance(type, size);

}public T get(intindex) {returnts[index];

}publicT[] rep() {returnts;

}public void set(intindex, T t) {

ts[index]=t;

}public static void main(String[] args) {

ArrayOfGeneric4 aog2 = new ArrayOfGeneric4(Integer.class, 10);

Object[] objs=aog2.rep();for (int i = 0; i < 10; i++) {

aog2.set(i, i);

System.out.println(aog2.get(i));

}try{

Integer[] strs=aog2.rep();

System.out.println("user Array.newInstance to create generci of array was successful!!!!! ");

}catch(Exception ex) {

ex.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值