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

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

问题描述

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

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

我用谷歌搜索并找到了 这个帖子,并进行了我认为会消除错误的更改,但我继续收到错误.

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

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

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 时,你对 的使用愚弄了我.

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天全站免登陆