无法在 Java 中创建...的通用数组
Cannot create a generic array of ... in Java
我收到一个我无法理解的错误。
我这样定义 Java 中的 class 对:
public class Couple<T1, T2>{
private T1 t1;
private T2 t2;
public Couple(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
public Couple() {
this.t1 = null;
this.t2 = null;
}
public T1 getFirst(){return t1;}
public T2 getSecond(){return t2;}
}
在另一个 class 中,我尝试以这种方式定义数组:
Couple<byte[], byte[]>[] res = new Couple<byte[], byte[]>[10];
但是 Eclipse 告诉我错误 "Cannot create a generic array of Couple<byte[], byte[]> "
。
好吧,我不明白的是我提到我想要几个字节[],所以在这一点上没有通用性,我错了吗?
Java 泛型没有具体化,这意味着 'implementations' 本身不是 类。不能创建泛型数组这一事实是该语言的固有限制。
我建议您使用列表而不是数组。
我收到一个我无法理解的错误。 我这样定义 Java 中的 class 对:
public class Couple<T1, T2>{
private T1 t1;
private T2 t2;
public Couple(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
public Couple() {
this.t1 = null;
this.t2 = null;
}
public T1 getFirst(){return t1;}
public T2 getSecond(){return t2;}
}
在另一个 class 中,我尝试以这种方式定义数组:
Couple<byte[], byte[]>[] res = new Couple<byte[], byte[]>[10];
但是 Eclipse 告诉我错误 "Cannot create a generic array of Couple<byte[], byte[]> "
。
好吧,我不明白的是我提到我想要几个字节[],所以在这一点上没有通用性,我错了吗?
Java 泛型没有具体化,这意味着 'implementations' 本身不是 类。不能创建泛型数组这一事实是该语言的固有限制。
我建议您使用列表而不是数组。