java 使用未经检查或不安全的操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16617901/
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
Uses unchecked or unsafe operations
提问by user2395468
I keep getting an error that says: Note: ABag.java uses unchecked or unsafe operations.
我不断收到一条错误消息:注意:ABag.java 使用未经检查或不安全的操作。
I googled it and found thispost, and made the changes that I thought would remove the error but I continue to get the error.
我用谷歌搜索并找到了这篇文章,并进行了我认为会消除错误的更改,但我仍然收到错误消息。
Is there anything else I can do to stop getting this error message?
我还能做些什么来停止收到此错误消息?
public class ABag<Item> implements BagInterface<Item>
{
private ArrayList<Item> bag;
//creates an empty bag
public ABag(){
bag = new ArrayList<Item>();
}
//creates an empty set with initial capacity
public ABag (int initialCapacity){
bag = new ArrayList<Item>(initialCapacity);
}
public boolean add(Item newEntry){
if (newEntry == null)
return false;
else
{
bag.add(newEntry);
return true;
}
}
public boolean isFull(){
return false;
}
public Item[] toArray(){
Item[] temp = (Item[])bag.toArray();
return temp;
}
public boolean isEmpty(){
return false;
}
public int getCurrentSize(){
return bag.size();
}
public int getFrequencyOf(Item anEntry){
int count = 0;
if (!(bag.contains(anEntry)))
{
for (int i=0;i<bag.size();i++)
{
if (bag.get(i) == anEntry)
count++;
}
}
return count;
}
public boolean contains(Item anEntry){
return bag.contains(anEntry);
}
public void clear(){
bag.clear();
}
public Item remove(){
int size = bag.size();
Item removed = bag.remove(size-1);
return removed;
}
public boolean remove(Item anEntry){
return bag.remove(anEntry);
}
}
Thank you in advance!
先感谢您!
回答by Lawrence Dol
You should enable linting to get verbose warnings about the specific problems:
您应该启用 linting 以获取有关特定问题的详细警告:
javac -Xlint:all ...
Among other things, toArray()
is broken. The List.toArray()
method returns an Object[], not an array of <T>
, so your cast to (Item[])
is incorrect and will fail at runtime. You should be using <T> T[] toArray(T[] a)
.
除其他外,toArray()
坏了。该List.toArray()
方法返回一个 Object[],而不是一个 的数组<T>
,因此您的转换(Item[])
不正确并且将在运行时失败。你应该使用<T> T[] toArray(T[] a)
.
In order to create an array of the generic type (possibly the biggest weakness of Java generics), you need to pass in the Class for the target type and use reflection and suppress the warning, like so:
为了创建泛型类型的数组(可能是 Java 泛型的最大弱点),您需要传入目标类型的 Class 并使用反射并抑制警告,如下所示:
static public <T> T[] create(Class<T> typ, int len) {
return uncheckedCast(java.lang.reflect.Array.newInstance(typ,len));
}
@SuppressWarnings("unchecked")
static public <T> T uncheckedCast(final Object obj) {
return (T)obj;
}
The other option is to push the problem back one layer to the code that can be assumed to know the correct type and pass an array of that type into your toArray
method, just as the Collections API does:
另一种选择是将问题推回到可以假定知道正确类型的代码的一层,并将该类型的数组传递到您的toArray
方法中,就像 Collections API 所做的那样:
public Item[] toArray(Item[] dummy) {
return this.bag.toArray(dummy);
}
As something of an aside, convention is to use a single uppercase letter for the generic type; your use of <Item>
fooled me at first when I was looking at toArray
in isolation.
顺便说一句,约定是对泛型类型使用单个大写字母;<Item>
起初,当我toArray
孤立地看时,你的使用愚弄了我。
回答by user114676
Replace your toArray with this, to avoid the cast
用这个替换你的 toArray,以避免强制转换
public Item[] toArray(){
Item[] temp = bag.toArray(new Item[0]);
return temp;
}