由于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;
}
但我真的不明白这是怎么回事。
在Java 8中,我们可以使用lambda或方法引用创建一种泛型数组。这类似于反射方法(传递一个类),但这里我们没有使用反射。
@FunctionalInterface
interface ArraySupplier<E> {
E[] get(int length);
}
class GenericSet<E> {
private final ArraySupplier<E> supplier;
private E[] array;
GenericSet(ArraySupplier<E> supplier) {
this.supplier = supplier;
this.array = supplier.get(10);
}
public static void main(String[] args) {
GenericSet<String> ofString =
new GenericSet<>(String[]::new);
GenericSet<Double> ofDouble =
new GenericSet<>(Double[]::new);
}
}
例如,<A> A[] Stream.toArray(IntFunction<A[]>)使用此方法。
这也可以在java 8之前使用匿名类来完成,但是比较麻烦。
也许与这个问题无关,但当我得到“通用数组创建”错误使用
Tuple<Long,String>[] tupleArray = new Tuple<Long,String>[10];
我用@SuppressWarnings({"unchecked"})找到了以下作品(并为我工作):
Tuple<Long, String>[] tupleArray = new Tuple[10];