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
}
在这里,我们需要讨论"checked" and "unchecked"。
Checked:strong typing。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]
}
}
Unchecked: weak typing。实际上没有对作为参数传递的任何对象进行类型检查。
在这种情况,我们需要这样写:
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中泛型一个的特性但也是一个weakness:它是使用擦除实现的,因此除非实施一些显式机制(type-checking),否则“泛型”类不知道它们在运行时创建的类型参数,故无法提供 type-safety。