使用未经检查或不安全的操作 [英] Uses unchecked or unsafe operations

查看:78
本文介绍了使用未经检查或不安全的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到一条错误消息: 注意:ABag.java使用未经检查或不安全的操作.

I keep getting an error that says: Note: ABag.java uses unchecked or unsafe operations.

我用Google搜索它,发现 这个帖子,并进行了我认为可以消除该错误的更改,但我继续收到该错误.

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.

我还能做些其他事情来停止收到此错误消息吗?

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);
}
}

提前谢谢!

推荐答案

您应该启用linting以获得有关特定问题的详细警告:

You should enable linting to get verbose warnings about the specific problems:

javac -Xlint:all ...

除其他事项外,toArray()已损坏. List.toArray()方法返回一个Object [],而不是<T>的数组,因此您对(Item[])的转换是不正确的,并且将在运行时失败.您应该使用<T> T[] toArray(T[] a).

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).

为了创建泛型类型的数组(这可能是Java泛型的最大弱点),您需要将Class传递给目标类型,并使用反射和抑制警告,例如:

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:

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;
}

另一种选择是像将Collections API那样,将问题推回至可以假定知道正确类型的代码的一层,并将该类型的数组传递到您的toArray方法中.

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:

public Item[] toArray(Item[] dummy) {
    return this.bag.toArray(dummy);
}

顺便说一句,约定是对通用类型使用单个大写字母.当我单独查看toArray时,您对<Item>的使用一开始就骗了我.

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.

这篇关于使用未经检查或不安全的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆