[英]How do I create a List of generic arrays in Java?
I've written a class which accepts a generic type, and I'm trying to create an array list of generic arrays within it. 我编写了一个接受通用类型的类,并试图在其中创建通用数组的数组列表。 I understand that Java can't create generic arrays , but I also know there are workarounds . 我知道Java无法创建通用数组 ,但是我也知道有解决方法 。 Is there a way the below code can work, or am I barking up the wrong tree? 下面的代码有什么方法可以工作,还是我吠错了树?
public class IterableContainer<T extends IterableItem> {
private T[] itemArray;
// how can i get this following line to work?
private List<T[]> items = new ArrayList<T[10]>();
public IterableContainer() {
... etc ...
Ignore past here - turns out it was an IDE issue. 忽略这里-事实证明这是一个IDE问题。
Left in for continuity of questions and answers. 留给问题和答案的连续性。
EDIT: 编辑:
This also doesn't work: 这也行不通:
private List<T[]> items = new ArrayList<T[]>();
with the error: 与错误:
Syntax error on token ">", VariableDeclaratorId expected after this token 令牌“>”上的语法错误,此令牌后应为VariableDeclaratorId
It works just fine, you just can't use the T[10]
declaration as the length of an array doesn't affect its type. 它工作得很好,您不能使用T[10]
声明,因为数组的长度不会影响其类型。
ie 即
... = new ArrayList<T[]>();
Not saying it's a great idea, but it should be possible with the same restrictions on generic arrays as always. 并不是说这是一个好主意,但是应该可以像往常一样对通用数组施加相同的限制。 Creating stuff to put in your list will give you a headache. 创建要放入列表的内容会让您头疼。
“ ...吠叫错误的树... ,使用List<List<T>>
。在Java中使用原始数组几乎总是一种代码味道,没有理由不使用适当的收集类。
private List<T[]> items = new ArrayList<T[]>();
works fine in my machine 在我的机器上工作正常
When you say "I'm developing for mobile devices" ....are you targeting j2me? 当您说“我正在为移动设备开发”时....您是否以j2me为目标? There is no support for generics in j2metargetng j2metargetng中不支持泛型
This is a valid declaration in java (according to spec) and compiles just fine with javac as others have commented. 这是Java中的有效声明(根据spec),并且可以使用javac进行编译,就像其他人评论的那样。
public class IterableContainer<T extends IterableItem> {
private T[] itemArray;
private List<T[]> items = new ArrayList<T[]>();// valid
..........
}
I believe the error you are seeing is not emitted from Eclipse, possibly coming from an Android SDK configured in Eclipse. 我相信您看到的错误不是从Eclipse发出的,可能是来自在Eclipse中配置的Android SDK。 If you create a Java Project in Eclipse, this code should work just fine. 如果您在Eclipse中创建Java项目,则此代码应该可以正常工作。 If you use this in an Android Project in Eclipse, you are likely to run into this one. 如果您在Eclipse的Android项目中使用此功能,则很可能会遇到此问题。 I had this error when running this code from an Android project : 从Android项目运行以下代码时出现此错误:
# guarantee(_name_index != 0 && _signature_index != 0) failed: bad constant pool index for fieldDescriptor
Sounds like you are restricted in an Android project, unfortunately. 不幸的是,听起来您受到Android项目的限制。
You have not defined T in this code. 您尚未在此代码中定义T。
If you are creating a generic class, you need to write: 如果要创建泛型类,则需要编写:
public class <T extends IterableItem> IterableContainer...
The next problem in your code is that you are trying to initilize items of ArrayList during its construction. 代码中的下一个问题是您正在尝试在ArrayList的构造过程中对其进行初始化。 It is impossible. 是不可能的。 You should rather write: 您应该写:
private List<T[]> items = new ArrayList<T[]>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.