Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Collections.sort() in Java 5

163 views
Skip to first unread message

alex_us01

unread,
Oct 16, 2005, 9:09:54 AM10/16/05
to
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

Thomas Hawtin

unread,
Oct 16, 2005, 10:07:48 AM10/16/05
to
alex_us01 wrote:
>
> 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.

> ---
>
> What am I missing?

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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

Ronny Schuetz

unread,
Oct 16, 2005, 10:24:27 AM10/16/05
to
alex_us01 wrote:

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

alex_us01

unread,
Oct 16, 2005, 11:06:43 AM10/16/05
to
Sorry, that was my typo when writing it here. It is alDecs in both
places and don't have problem compiling it. Thanks.
alex

Message has been deleted
Message has been deleted

alex_us01

unread,
Oct 16, 2005, 11:19:37 AM10/16/05
to
Thanks!! That was it: Decision needs to implement Comparable<Decision>.
I didn't think about this, I really appreciate it!

alex

Roedy Green

unread,
Oct 16, 2005, 11:34:15 AM10/16/05
to
On 16 Oct 2005 06:09:54 -0700, "alex_us01" <aris...@gmail.com> wrote
or quoted :

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

You sail alDec in the first line and alDecs in the second.

Your Comparable needs to be generified too. See
http://mindprod.com/jgloss/sort.html
http://mindprod.com/jgloss/comparable.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

0 new messages