【问题标题】:Project9.java uses unchecked or unsafe operations [duplicate]Project9.java 使用未经检查或不安全的操作 [重复]
【发布时间】:2013-07-30 20:37:59
【问题描述】:
public class Project9
{
    public static void main(String args[])
    {
        InventoryItem[] items = new InventoryItem[3];

        items[0] = new InventoryItem("Coffee", 1);
        items[1] = new InventoryItem("Pencils", 2);
        items[2] = new InventoryItem("Notebooks", 3);

        System.out.println("Before sorting");
        System.out.println(items[0]);
        System.out.println(items[1]);
        System.out.println(items[2]);

        InventoryItem.sort(items, items.length);

        System.out.println("After sorting");
        System.out.println(items[0]);
        System.out.println(items[1]);
        System.out.println(items[2]);
    }
}

class InventoryItem implements Comparable<InventoryItem>
{
    private String name;
    private int uniqueItemID;

    public InventoryItem()
    {
        name = " ";
        uniqueItemID = 0;
    }

    public InventoryItem(String newName, int newItemID)
    {
        name = newName;
        uniqueItemID = newItemID;
    }

    public InventoryItem(InventoryItem i)
    {
        name = i.name;
        uniqueItemID = i.uniqueItemID;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    public void setItemID(int newItemID)
    {
        uniqueItemID = newItemID;
    }

    public int getItemID()
    {
        return uniqueItemID;
    }

    public String getName()
    {
        return name;
    }

    public int compareTo(InventoryItem i)
    {
        int anotherUniqueID = ((InventoryItem) i).getItemID();
        return (this.uniqueItemID - anotherUniqueID);
    }

    public static void sort(Comparable[] a, int numberUsed)
    {
        int index, indexOfNextSmallest;

        for(index = 0; index < numberUsed - 1; index++)
        {
            indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
            interchange(index, indexOfNextSmallest, a);
        }
    }

    private static int indexOfSmallest(int startIndex, Comparable[] a, int numberUsed)
    {
        Comparable min = a[startIndex];
        int indexOfMin = startIndex;
        int index;

        for(index = startIndex + 1; index < numberUsed; index++)
        {
            if(a[index].compareTo(min) < 0)
            {
                min = a[index];
                indexOfMin = index;
            }
        }
        return indexOfMin;
    }

    private static void interchange(int i, int j, Comparable[] a)
    {
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

当我编译我的代码时,它会抛出错误消息“Project9.java 使用未经检查或不安全的操作”。

当我按照它的建议使用 Xlint:unchecked 重新编译时,我收到这条消息“警告:[unchecked] unchecked call to compareTo(T) as a member of raw type Comparable

        if(a[index].compareTo(min) < 0)
                             ^

其中 T 是一个类型变量:

T extends Object declared in interface Comparable

我需要做什么/理解什么才能解决这个问题?非常感谢!

【问题讨论】:

  • 请不要粘贴代码链接,而是粘贴代码本身。我冒昧地为你做这件事,但下一次,如果你愿意自己做这件事,我们将不胜感激。
  • 请注意,您看到的是警告,而不是错误。您的代码将编译,但存在一些风险,因为您没有使用泛型。现在,如果你还没有学习泛型,你可以忽略这个警告。要么,要么去泛型教程学习它们。
  • 我将如何使用泛型?我在入门级 OOP 课程中,不认识该术语。
  • 谷歌教程。您的 a 变量是 Comparable,而不是 Comparable&lt;Inventory&gt; 类型。考虑将其声明为后者。

标签: java unsafe operations unchecked


【解决方案1】:

也许将演员表添加到 InventoryItem?

private static int indexOfSmallest(int startIndex, Comparable<InventoryItem>[] a, int numberUsed)
{
    Comparable<InventoryItem> min = a[startIndex];
    int indexOfMin = startIndex;
    int index;

    for(index = startIndex + 1; index < numberUsed; index++)
    {
        if(a[index].compareTo((InventoryItem) min) < 0)
        {
            min = a[index];
            indexOfMin = index;
        }
    }
    return indexOfMin;
}

【讨论】:

  • 不幸的是,我仍然遇到同样的错误。
  • 不,泛型专门用于避免这种强制转换的需要。
【解决方案2】:

不要使用 Comparable 作为方法参数,而是使用 InventoryItem。

例如:

private static int indexOfSmallest(int startIndex, InventoryItem[] a, int numberUsed)
{
    InventoryItem min = a[startIndex];
    int indexOfMin = startIndex;
    int index;

    for(index = startIndex + 1; index < numberUsed; index++)
    {
        if(a[index].compareTo(min) < 0)
        {
            min = a[index];
            indexOfMin = index;
        }
    }
    return indexOfMin;
}

private static void interchange(int i, int j, InventoryItem[] a)
{
    InventoryItem temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

这样编译器可以进行编译时类型检查,以确保您的参数是正确的类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多