This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.)
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?
author and iconoclast
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?
Joined: Sep 24, 2003
I was able to duplicate the compiler warning message by creating a small test application. See below: