没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
在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)));
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";
}
}
上面的这个例子比较简单,但是如果你有接触过泛型数组,你便对他的复杂度有一定的了解,由于创建泛型数组比较复杂,所以在实际的应用过程中一般会选择List的对泛型进行存储,如果实在需要使用泛型数组,则需要注意数组的在运行时的类型,think in java这本书中,对泛型数组的处理通过四个小程序对其进行了比较完整的描述。
程序一:这个程序主要说明了,在使用泛型数组中容易出现的问题,由于书中对于程序的说明比较详细,所以只对程序做引用。
class Generic<T> {
}
public class ArrayofGeneric {
public static void main(String[] args) {
Generic<Integer>[] genArr;
/*
* will throw ClassCastException :The problem is that arrays keep track of their actual type, and that type is
* established at the point of creation of the array. So even though genArr has been cast to a Generic < Integer
* >[] , that information only exists at compile time (and without the @SuppressWarnings annotation, you’d get a
* warning for that cast). At run time, it’s still an array of Object, and that causes problems.
*/
// genArr = (Generic<Integer>[]) new Object[] {};
/* can not create a generic of array */
}
}
class Type {
@Override
public String toString() {
return "type";
}
}
上面的这个例子比较简单,但是如果你有接触过泛型数组,你便对他的复杂度有一定的了解,由于创建泛型数组比较复杂,所以在实际的应用过程中一般会选择List的对泛型进行存储,如果实在需要使用泛型数组,则需要注意数组的在运行时的类型,think in java这本书中,对泛型数组的处理通过四个小程序对其进行了比较完整的描述。
程序一:这个程序主要说明了,在使用泛型数组中容易出现的问题,由于书中对于程序的说明比较详细,所以只对程序做引用。
class Generic<T> {
}
public class ArrayofGeneric {
public static void main(String[] args) {
Generic<Integer>[] genArr;
/*
* will throw ClassCastException :The problem is that arrays keep track of their actual type, and that type is
* established at the point of creation of the array. So even though genArr has been cast to a Generic < Integer
* >[] , that information only exists at compile time (and without the @SuppressWarnings annotation, you’d get a
* warning for that cast). At run time, it’s still an array of Object, and that causes problems.
*/
// genArr = (Generic<Integer>[]) new Object[] {};
/* can not create a generic of array */
剩余7页未读,继续阅读












资源评论

黑谷子
- 粉丝: 7
- 资源: 12

上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
已下载
下载帮助

看过该资源的人还看了
安全验证
文档复制为VIP权益,开通VIP直接复制
