由于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;
}

但我真的不明白这是怎么回事。


当前回答

实际上,我找到了一个非常独特的解决方案来绕过无法初始化泛型数组的问题。你要做的是创建一个类,它接受泛型变量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++;
}

其他回答

这是类型安全的唯一答案

E[] a;

a = newArray(size);

@SafeVarargs
static <E> E[] newArray(int length, E... array)
{
    return Arrays.copyOf(array, length);
}

你可以这样做:

E[] arr = (E[])new Object[INITIAL_ARRAY_LENGTH];

这是在Effective Java中实现泛型集合的建议方法之一;26项。没有类型错误,不需要重复强制转换数组。然而,这会触发警告,因为它有潜在的危险,应该谨慎使用。正如注释中所详细描述的,这个对象[]现在伪装成我们的E[]类型,如果不安全使用,可能会导致意外错误或classcastexception。

根据经验,只要强制转换数组在内部使用(例如支持数据结构),并且不返回或暴露给客户端代码,这种行为是安全的。如果需要向其他代码返回泛型类型的数组,则您提到的反射array类是正确的方法。


值得一提的是,在任何可能的情况下,如果您使用泛型,那么使用列表将比使用数组更愉快。当然,有时您别无选择,但使用集合框架要健壮得多。

实际上,更简单的方法是创建一个对象数组,并将其转换为所需的类型,如下例所示:

T[] array = (T[])new Object[SIZE];

其中SIZE是一个常量,T是一个类型标识符

在java中不允许创建通用数组,但是你可以这样做

class Stack<T> {
private final T[] array;
public Stack(int capacity) {
    array = (T[]) new Object[capacity];
 }
}

试试这个。

private int m = 0;
private int n = 0;
private Element<T>[][] elements = null;

public MatrixData(int m, int n)
{
    this.m = m;
    this.n = n;

    this.elements = new Element[m][n];
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            this.elements[i][j] = new Element<T>();
        }
    }
}