• 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

dealing with compiler warnings

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I'm using JDK 1.5 and created an instance of an ArrayList, then after loading the ArrayList I passed it as an arguement to the Collections.sort(...) method. When I compile using:

javac -Xlint:unchecked -classpath "C:\Program Files\Apache Softwa
re Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\Program Files\Apache Soft
ware Foundation\Tomcat 5.5\common\lib\jsp-api.jar;C:\Program Files\Apache Softwa
re Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\classes;C:\Program Files\Apa
che Software Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\classes\mvcs;C:\Pr
ogram Files\Apache Software Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\cla
sses\com\components;C:\Program Files\Apache Software Foundation\Tomcat 5.5\webap
ps\scholastic\WEB-INF\classes\com\ezjavabeans\ezsmtp;C:\Program Files\Apache Sof
tware Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\lib\jasperreports-0.6.7.j
ar" *.java

I got the following warning message:

TestProcessor.java:153: warning: [unchecked] unchecked method invocation: <T>sor
t(java.util.List<T> in java.util.Collections is applied to (java.util.ArrayList
<mvcs.TestProcessor.Answer>
Collections.sort(list);
^
1 warning

What is it I need to do further to prevent this warning message from popping up all the time? Am I supposed to ignore it or is there something I'm expected to do to prevent this warning message?

Please advise,

Alan
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a word: Generics. Rather than using just an ArrayList, you can and should use an ArrayList<Widget> -- an ArrayList of Widgets (where "widget" stands in for whatever kind of object you're actually using.)
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did use Generics with my code. I defined a class named Answer and loaded up several instances of Answer into the ArrayList:

ArrayList<Answer> list = new ArrayList<Answer>();

for(int i = 0; i < ...; i++)
list.add(new Answer(...));
...

Collections.sort(list);

So, you see, even though I defined a specific type for ArrayList, I still get the error message described earlier.

Alan
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Provide the code (or a derivation of) that you allege is producing the problem. Collections.sort is a parameterised method where the type T is inferred.
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Here's the majority of my code from a servlet I'm working on. First I define an inner class named Answer, then you'll see the beginning of the doPost() method right up to the point where I call Collections.sort(list);
It's that method call that is causing the warning message during compilation. Does this help any?

Alan


 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! OK, here's the deal: Foo implements the unsafe version of Comparable, and that's what's causing the warning -- i.e., it's an ArrayList<Comparable> rather than an ArrayList<Comparable<Foo>>. You need to make two small changes:

1) class Answer implements Comparable<Answer>

2) public int compareTo(Answer ans) ...

and the warning will go away. Sorry I didn't think of this before. The error message is really somewhat inaccurate, isn't it?
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to duplicate the compiler warning message by creating a small test application. See below:

 
Arthur, where are your pants? Check under this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic