• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

warning: [unchecked] unchecked method invocation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. Apologies if this should be obvious. When I compile the following -Xlint I'm warned:

CompareMe.java:27: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T> ;) in java.util.Collections is applied to (java.util.List<scratch.Item> ;)
Collections.sort(list);
^
1 warning

list is declared to hold only <Items>, and I assume Collections.sort is a generic method. What am I missing?

Thanks much.


[ June 18, 2008: Message edited by: Mike Mitchell ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to make Item implement Comparable<Item>, not just Comparable (which is a raw type). That is:
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public int compareTo(Object o) { Item i = (Item) o; if (this.number < i.number) { return -1; } else if (this.number > i.number) { return 1; } else { return 0; } }



Hi

compareTo() method's signature must be the same, as you are overriding it and not overloading. That means, you are implementing the method from the interface Comparable.

public int compareTo(Object o) {
Item i = (Item) o;
if (this.number < i.number) {
return -1;
} else if (this.number > i.number) {
return 1;
} else {
return 0;
} }

and not

public int compareTo(Item o) {
Item i = (Item) o;
if (this.number < i.number) {
return -1;
} else if (this.number > i.number) {
return 1;
} else {
return 0;
} }

Kind Regards
Kris
 
Mike Mitchell
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much Matt and Kris -- that got me straightened out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic