简体   繁体   English

使用未经检查或不安全的操作

[英]Uses unchecked or unsafe operations

I keep getting an error that says: Note: ABag.java uses unchecked or unsafe operations. 我不断收到错误消息:注意:ABag.java使用未经检查或不安全的操作。

I googled it and found this post, and made the changes that I thought would remove the error but I continue to get the error. 我在Google上搜索并找到了这篇文章,并进行了我认为可以消除该错误的更改,但我仍然收到该错误。

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! 先感谢您!

You should enable linting to get verbose warnings about the specific problems: 您应该启用linting以获得有关特定问题的详细警告:

javac -Xlint:all ...

Among other things, toArray() is broken. 除其他外, toArray()已损坏。 The List.toArray() method returns an Object[], not an array of <T> , so your cast to (Item[]) is incorrect and will fail at runtime. List.toArray()方法返回一个Object [],而不是<T>的数组,因此您对(Item[])的转换不正确,并且在运行时将失败。 You should be using <T> T[] toArray(T[] a) . 您应该使用<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. 当我toArray查看toArray时,您对<Item>使用一开始就骗了我。

Replace your toArray with this, to avoid the cast 以此替换您的toArray,以避免强制转换

public Item[] toArray(){
    Item[] temp = bag.toArray(new Item[0]);
    return temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM