Collections.sort() in Java 5

A

alex_us01

hello,

Probably has a simple solution but I couldn't see it.
I still get an "unchecked" warning for this:

---
ArrayList<Decision> alDec = new ArrayList<Decision>();
....
// By the way, Decision implements Comparable interface
....
Collections.sort(alDecs); // here I get the unchecked warning
---

What am I missing?

thanks,
alex
 
T

Thomas Hawtin

alex_us01 said:
Probably has a simple solution but I couldn't see it.
I still get an "unchecked" warning for this:

---
ArrayList<Decision> alDec = new ArrayList<Decision>();
...
// By the way, Decision implements Comparable interface
...
Collections.sort(alDecs); // here I get the unchecked warning ^^^^^^ It's alDec above.

o The actual code you tried to compile, preferably as a short,
complete, compilable example.
o The actual error given.
o Details of the Decision type.

I guess the error message is something like:


Dec.java:6: warning: [unchecked] unchecked method invocation:
<T>sort(java.util.List<T>) in java.util.Collections is applied to
(java.util.ArrayList<Decision>)
Collections.sort(alDecs);
^
1 warning


Note, javac doesn't tell us what the problem was. But if I reveal the
Decision class I broke to force the error:

abstract class Decision implements Comparable { }

There's your problem. The Comparable interface takes a generic
parameter, which is missing.

abstract class Decision implements Comparable<Decision> { }

Tom Hawtin
 
R

Ronny Schuetz

alex_us01 said:
Probably has a simple solution but I couldn't see it.
I still get an "unchecked" warning for this:

---
ArrayList<Decision> alDec = new ArrayList<Decision>();
...
// By the way, Decision implements Comparable interface
...
Collections.sort(alDecs); // here I get the unchecked warning

Decision needs to implement Comparable<Decision> instead of
Comparable.
 
A

alex_us01

Sorry, that was my typo when writing it here. It is alDecs in both
places and don't have problem compiling it. Thanks.
alex
 
A

alex_us01

(I mean Decision needs to implement Comparable<Decision>, that
eliminated the warning.)
 
A

alex_us01

Thanks!! That was it: Decision needs to implement Comparable<Decision>.
I didn't think about this, I really appreciate it!

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top