|
-
Nov 11th, 2006, 07:04 PM
#1
Thread Starter
Frenzied Member
Collections.sort() and Generics?
I'm getting a warning when calling collections.sort() when passing an ArrayList
containing instances of subclasses of an abstract class.
Code:
import java.lang.Comparable;
public abstract class Person<V extends Comparable> implements Comparable<V> {
public String name = "";
public int age = 0;
public Person() {
}
public int compareTo(V o) {
return this.age - o.age;
}
} // Person
Code:
public class Drunk extends Person {
public double BAC = 0;
public Drunk() {
super();
}
} // Drunk
Code:
import java.lang.Comparable;
import java.util.ArrayList;
import java.util.Collections;
public class GenericsTest {
public static void main(String[] args) {
ArrayList<Person> ppl = new ArrayList<Person>();
Drunk myDrunk1 = new Drunk();
Drunk myDrunk2 = new Drunk();
ppl.add(myDrunk1);
ppl.add(myDrunk2);
Collections.sort(ppl);
}
} // GenericsTest
Produces the warning: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.ArrayList<Person>)
Collections.sort(ppl);
^
I think there is something wrong with the way I'm declaring the abstract class Person.
Last edited by <ABX; Nov 11th, 2006 at 07:24 PM.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Nov 11th, 2006, 07:17 PM
#2
Re: Collections.sort() and Generics?
Person shouldn't be generic in the first place. It should just be
Code:
class Person implements Comparable<Person>
...
public int compareTo(Person o)
...
Then see if the problem persists.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2006, 07:24 PM
#3
Thread Starter
Frenzied Member
Re: Collections.sort() and Generics?
Hrm... that makes much more sense. It seems I need to re-read a chapter or two :P
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|