简体   繁体   English

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

[英]InventoryItem.java uses unchecked or unsafe operations

I'm learning about comparable and am implementing it in my Inventory class. 我正在学习可比性,并将其在我的广告资源类中实现。 However when I go to compile the code, the compiler gives an error. 但是,当我去编译代码时,编译器给出了一个错误。

InventoryItem.java uses unchecked or unsafe operations.

Can anyone please help me out. 谁能帮我一下。 What is wrong with my code and what can I do to fix this issue. 我的代码有什么问题,我该怎么做才能解决此问题。 Thank you for your help in advance. 提前谢谢你的帮助。

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

public class InventoryItemTester
{
    public static void main(String [] args)
    {
            InventoryItem[] items = new InventoryItem[3];

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

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

At a guess I'd say this line is causing the issue (Your compiler tells you exactly which line is the problem, this might be useful to include in your next question): 猜想我会说这是问题的根源(您的编译器会告诉您确切的问题是哪一行,将其包含在下一个问题中可能会很有用):

   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++)
        {
here==========> if(a[index].compareTo(min) < 0)
                {

You are calling compareTo with a InventoryItem where it is expecting an Object . 您正在使用需要一个ObjectInventoryItem调用compareTo。 You could add a @SuppressWarnings annotation which would make it go away :) 您可以添加一个@SuppressWarnings批注,以使其消失:)

The basic idea of Comparable and Comparator is they apply a sorting order to an Object so that the standard JDK Collections objects can do all the hard work for you. Comparable和Comparator的基本思想是将排序顺序应用于对象,以便标准JDK Collections对象可以为您完成所有艰苦的工作。

In your case your comparesTo method does the correct thing, however I'm not sure if this is good planning or good luck, so things to note: 在您的情况下,您的comparesTo方法可以执行正确的操作,但是我不确定这是好的计划还是好运,因此要注意以下几点:

  1. InventoryItem.comparesTo method needs to evaluate the current instance to the provided instance and return an integer signifying the ordering, -1 means the instance (ie this ) should be ordered before the argument, 0 means they are the same and 1 means the instance is after the argument. InventoryItem.comparesTo方法需要将当前实例评估为所提供的实例,并返回一个表示顺序的整数; -1表示实例(即this )应在参数前排序; 0表示它们相同; 1表示实例是论点之后。 Something like this lets the JDK do all the hard work for you 这样的事情让JDK为您完成所有艰苦的工作

     public int compareTo(InventoryItem i) { return Integer.valueOf(this.uniqueItemID).compareTo(i.uniqueItemID); } 
  2. In order to use Comparable all you really need to do is implement it and then use the standard JDK Collections classes to do all the heavy lifting for you, eg: 为了使用Comparable,您真正需要做的就是实现它,然后使用标准的JDK Collections类为您完成所有繁重的工作,例如:

     import java.util.ArrayList; import java.util.Collections; import java.util.List; public class InventoryItemTest { public static void main(String [] args) { List<InventoryItem> items = new ArrayList<InventoryItem>(); items.add(new InventoryItem("Pens", 2)); items.add(new InventoryItem("Pencils", 3)); items.add(new InventoryItem("Notebooks", 1)); System.out.println("Before sorting"); System.out.println(items); Collections.sort(items); System.out.println("After sorting"); System.out.println(items); } } 
  3. I realise this might not be as much fun as writing your own sorting algorithms but if you want to do that with no compiler warnings then you need to start looking at generics 我意识到这可能不如编写自己的排序算法那么有趣,但是如果您想在没有编译器警告的情况下这样做,那么您需要开始研究泛型

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

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