0

I am aware that there is number of similar posts. The error, as i understand, means that i should be more specific with types. My code:

import java.util.*;
public class Storefront {
    private  LinkedList<Item> catalog = new LinkedList<Item>();

    public void addItem(String id, String name, String price, String quant) {
         Item it = new Item(id, name, price, quant);
         catalog.add(it);
    }

    public Item getItem(int i) {
            return (Item)catalog.get(i);
    }

    public int getSize() {
         return catalog.size();
    }

    //@SuppressWarnings("unchecked")
    public void sort() {
        Collections.sort(catalog);
    }
}

However, I do specify that LinkedList consists of objects of type Item. When i compile it with -xlint, i get


warning: unchecked method invocation: method sort in class
Collections is applied to given types
Collections.sort(catalog);

required: List'<'T'>'

found: LinkedList'<'Item'>'

where T is a type-variable:
T extends Comparable'<'? super T'>' declared in method 
'<'T'>'sort'<'List'<'T'>'>

As i understand, LinkedList implements List and Item implements Comparable. So, aren't 'required' and 'found' same?

Also I was checking if catalog.get(i); is actually an Item (as, some say it may have caused the problem), but it produced the same error.

webermaster
  • 209
  • 1
  • 13
StackExploded
  • 519
  • 6
  • 17

1 Answers1

5

You get this warning if your Item class implements Comparable and not Comparable<Item>. Make sure your Item class is defined like this:

class Item implements Comparable<Item> {
Keppil
  • 43,696
  • 7
  • 86
  • 111
  • 1
    Don't forget to do a "compareTo(Item I)" (or similar) instead of generic "Object". It's real easy to forget to do that when going back and updating old code. – Brian Knoblauch Nov 18 '14 at 14:54