"Java 泛型数组的创建方法和注意事项" 在Java中,由于类型擦除的原因,我们无法直接使用 `T[] tarr = new T[10]` 的方式来创建泛型数组。这是因为泛型是编译时的概念,到运行时会被擦除,导致在运行时无法获取到具体的类型信息。因此,Java提供了一种替代方法,即使用 `Array.newInstance(Class<T> type, int size)` 方法来创建泛型数组。 以下是一个示例代码,展示了如何使用 `Array.newInstance()` 创建泛型数组: ```java 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>(); } public static void main(String[] args) { // 即使 kind 存储为 Class<T>,由于类型擦除,实际上它只是一个 Class 类,没有参数。 // 因此,当你使用它,如创建数组时,Array.newInstance() 并不具有暗示在 kind 中的类型信息,所以它无法生成特定的结果,这需要强制转换,会产生一个无法满足的警告。 ArrayMaker<Type> 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"; } } ``` 在这个例子中,`ArrayMaker` 类包含一个构造函数用于接收 `Class<T>` 类型的参数,然后在 `createArray` 方法中使用 `Array.newInstance()` 创建数组。需要注意的是,由于类型擦除,`Array.newInstance()` 返回的对象实际上是 `Object` 类型的数组,所以需要进行类型转换 `(T[])`,这会引发编译警告,因为这种转换可能在运行时抛出 `ClassCastException`。 此外,虽然泛型数组创建较为复杂,但我们可以使用 `ArrayList` 或其他集合类来代替,它们在内部处理了类型安全问题,提供了更加方便且安全的泛型容器,例如 `createList` 方法就返回了一个 `ArrayList<T>`。 Java中的泛型数组创建涉及到类型擦除和强制类型转换,虽然可以实现,但通常建议使用集合类来管理泛型对象,以避免类型安全问题和编译警告。对于需要数组的场景,可以考虑使用非泛型的数组或集合类,并在必要时进行类型检查。
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)));
}
}
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币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C++标准程序库:权威指南
- Java解惑:奇数判断误区与改进方法
- C++编程必读:20种设计模式详解与实战
- LM3S8962微控制器数据手册
- 51单片机C语言实战教程:从入门到精通
- Spring3.0权威指南:JavaEE6实战
- Win32多线程程序设计详解
- Lucene2.9.1开发全攻略:从环境配置到索引创建
- 内存虚拟硬盘技术:提升电脑速度的秘密武器
- Java操作数据库:保存与显示图片到数据库及页面
- ISO14001:2004环境管理体系要求详解
- ShopExV4.8二次开发详解
- 企业形象与产品推广一站式网站建设技术方案揭秘
- Shopex二次开发:触发器与控制器重定向技术详解
- FPGA开发实战指南:创新设计与进阶技巧
- ShopExV4.8二次开发入门:解决升级问题与功能扩展