由于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;
}
但我真的不明白这是怎么回事。
我找到了一个解决这个问题的方法。
下面这行代码抛出泛型数组创建错误
List<Person>[] personLists=new ArrayList<Person>()[10];
但是,如果我将List<Person>封装在一个单独的类中,它就可以工作。
import java.util.ArrayList;
import java.util.List;
public class PersonList {
List<Person> people;
public PersonList()
{
people=new ArrayList<Person>();
}
}
您可以通过getter在PersonList类中公开人员。下面的行将给出一个数组,每个元素中都有List<Person>。也就是说数组List<Person>。
PersonList[] personLists=new PersonList[10];
我需要这样的东西在一些代码中,我正在工作,这就是我所做的让它工作。到目前为止还没有问题。
实际上,我找到了一个非常独特的解决方案来绕过无法初始化泛型数组的问题。你要做的是创建一个类,它接受泛型变量T,就像这样:
class GenericInvoker <T> {
T variable;
public GenericInvoker(T variable){
this.variable = variable;
}
}
然后在你的数组类中让它像这样开始:
GenericInvoker<T>[] array;
public MyArray(){
array = new GenericInvoker[];
}
启动一个新的泛型调用器[]将导致一个未检查的问题,但实际上不应该有任何问题。
要从数组中获取,你应该调用数组[i]。变量如下:
public T get(int index){
return array[index].variable;
}
剩下的,比如调整数组的大小可以用Arrays.copyOf()来完成,如下所示:
public void resize(int newSize){
array = Arrays.copyOf(array, newSize);
}
add函数可以这样添加:
public boolean add(T element){
// the variable size below is equal to how many times the add function has been called
// and is used to keep track of where to put the next variable in the array
arrays[size] = new GenericInvoker(element);
size++;
}
我必须反过来问一个问题:你的发电机组是“检查”还是“未检查”?
这是什么意思?
Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of type E. See Collections.checkedCollection.
-> in that case, you should write:
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. No type checking is actually done on any of the objects passed as argument.
-> in that case, you should write
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;
}
}
Note that the component type of the array should be the erasure of the type parameter:
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中泛型的一个已知且刻意的弱点:它是使用擦除来实现的,因此“泛型”类不知道它们在运行时是用什么类型参数创建的,因此不能提供类型安全,除非实现了一些显式机制(类型检查)。