Java 错误:通用数组创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3865946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Generic Array Creation
提问by Luron
I don't understand the error of Generic Array Creation.
First I tried the following:
我不明白通用数组创建的错误。
首先我尝试了以下方法:
public PCB[] getAll() {
PCB[] res = new PCB[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
list.clear();
return res;
}
Then I tried doing this:
然后我尝试这样做:
PCB[] res = new PCB[100];
I must be missing something cause that seems right. I tried looking it up I really did. And nothing is clicking.
我一定错过了一些看起来正确的原因。我试着查了一下,我真的查到了。什么都没有点击。
My question is: What can I do to fix this?
我的问题是:我能做些什么来解决这个问题?
the error is :
错误是:
.\Queue.java:26: generic array creation
PCB[] res = new PCB[200];
^
Note: U:\Senior Year\CS451- file
uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Tool completed with exit code 1
工具完成,退出代码 1
回答by erickson
You can't create arrays with a generic component type.
您不能创建具有通用组件类型的数组。
Create an array of an explicit type, like Object[], instead. You can then cast this to PCB[]if you want, but I don't recommend it in most cases.
创建一个显式类型的数组,例如Object[], 。PCB[]如果你愿意,你可以将它转换为,但在大多数情况下我不推荐它。
PCB[] res = (PCB[]) new Object[list.size()]; /* Not type-safe. */
If you want type safety, use a collection like java.util.List<PCB>instead of an array.
如果您想要类型安全,请使用类似集合java.util.List<PCB>而不是数组。
By the way, if listis already a java.util.List, you should use one of its toArray()methods, instead of duplicating them in your code. This doesn't get your around the type-safety problem though.
顺便说一下,如果list已经是 a java.util.List,你应该使用它的toArray()方法之一,而不是在你的代码中复制它们。但这并不能解决您的类型安全问题。
回答by Mark Peters
Besides the way suggested in the "possible duplicate", the other main way of getting around this problem is for the array itself (or at least a template of one) to be supplied by the caller, who will hopefully know the concrete type and can thus safely create the array.
除了“可能的重复”中建议的方法之外,解决此问题的另一种主要方法是由调用者提供数组本身(或至少一个模板),调用者希望知道具体类型并且可以从而安全地创建数组。
This is the way methods like ArrayList.toArray(T[])are implemented. I'd suggest you take a look at that method for inspiration. Better yet, you should probably be using that method anyway as others have noted.
这就是方法 likeArrayList.toArray(T[])的实现方式。我建议你看看这种方法以获得灵感。更好的是,正如其他人所指出的那样,您应该无论如何都应该使用该方法。
回答by gdejohn
The following will give you an array of the type you want while preserving type safety.
下面将为您提供所需类型的数组,同时保持类型安全。
PCB[] getAll(Class<PCB[]> arrayType) {
PCB[] res = arrayType.cast(java.lang.reflect.Array.newInstance(arrayType.getComponentType(), list.size()));
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
list.clear();
return res;
}
How this works is explained in depth in my answerto the question that Kirk Woll linked as a duplicate.

