由于 Java 泛型的实现,您不能拥有这样的代码:
public class GenSet<E> {
private E a[];
public GenSet() {
a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation
}
}
如何在保持类型安全的同时实现这一点?
我在 Java 论坛上看到了这样的解决方案:
import java.lang.reflect.Array;
class Stack<T> {
public Stack(Class<T> clazz, int capacity) {
array = (T[])Array.newInstance(clazz, capacity);
}
private final T[] array;
}
但我真的不知道发生了什么。
我必须回答一个问题:您的GenSet
“已选中” 还是 “未选中”?那是什么意思?
检查 : 强类型 。 GenSet
明确知道它包含哪种类型的对象(即它的构造函数是使用Class<E>
参数显式调用的,并且当它们传递非E
类型的参数时,方法将抛出异常。请参阅Collections.checkedCollection
。
- > 在这种情况下,你应该写:
public class GenSet<E> {
private E[] a;
public GenSet(Class<E> c, int s) {
// Use Array native method to create array
// of a type only known at run time
@SuppressWarnings("unchecked")
final E[] a = (E[]) Array.newInstance(c, s);
this.a = a;
}
E get(int i) {
return a[i];
}
}
未选中 : 弱类型 。实际上没有对作为参数传递的任何对象进行类型检查。
- > 在这种情况下,你应该写
public class GenSet<E> {
private Object[] a;
public GenSet(int s) {
a = new Object[s];
}
E get(int i) {
@SuppressWarnings("unchecked")
final E e = (E) a[i];
return e;
}
}
请注意,数组的组件类型应该是类型参数的擦除 :
public class GenSet<E extends Foo> { // E has an upper bound of Foo
private Foo[] a; // E erases to Foo, so use Foo[]
public GenSet(int s) {
a = new Foo[s];
}
...
}
所有这些都源于 Java 中泛型的已知且有意识的弱点:它是使用擦除实现的,因此 “泛型” 类不知道它们在运行时创建的类型参数,因此无法提供类型 - 安全,除非实施一些显式机制(类型检查)。