compiler errors - InventoryItem.java uses unchecked or unsafe operations -


i'm learning comparable , implementing in inventory class. when go compile code, compiler gives error.

inventoryitem.java uses unchecked or unsafe operations.

can please me out. wrong code , can fix issue. thank 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 guess i'd line causing issue (your compiler tells line problem, might useful include in 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 calling compareto inventoryitem expecting object. add @suppresswarnings annotation make go away :)

the basic idea of comparable , comparator apply sorting order object standard jdk collections objects can hard work you.

in case comparesto method correct thing, i'm not sure if planning or luck, things note:

  1. inventoryitem.comparesto method needs evaluate current instance provided instance , return integer signifying ordering, -1 means instance (ie this) should ordered before argument, 0 means same , 1 means instance after argument. lets jdk hard work you

    public int compareto(inventoryitem i) {     return integer.valueof(this.uniqueitemid).compareto(i.uniqueitemid); } 
  2. in order use comparable need implement , use standard jdk collections classes heavy lifting you, eg:

    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 might not fun writing own sorting algorithms if want no compiler warnings need start looking @ generics


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -