
Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Giordano wrote, quoted or indirectly quoted someone who said :
>> ArrayList<K> list = new ArrayList<K>();
>> /* ERROR --> */ java.util.Collections.sort ( list );
> hint. Look at the Javadoc for Collections.sort.
>
[quoted text clipped - 5 lines]
> In other words T had bloody well better implement Comparable in a
> generic way.
What do you mean by "implement Comparable in a generic way"?
Here's a prototypical use of Collections.sort(), with T = String,
output:
tangled: { first, last, always, never, maybe, }
sorted: { always, first, last, maybe, never, }
<sscce name="SortOf.java">
package testit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SortOf
{
private static final String [] TANGLED = new String[]
{ "first", "last", "always", "never", "maybe", };
static <T> void show( List <T> list )
{
System.out.print( "{ " );
for ( T t : list )
{
System.out.print( t );
System.out.print( ", " );
}
System.out.println( "}" );
}
public void demonstrate()
{
List<String> strangs = new ArrayList <String> ();
strangs.addAll( Arrays.asList( TANGLED ));
System.out.print( "tangled: " );
show( strangs );
Collections.sort( strangs );
System.out.print( " sorted: " );
show( strangs );
}
public static void main( String[] args )
{
(new SortOf()).demonstrate();
}
}
</sscce>

Signature
Lew
Roedy Green - 23 May 2008 08:53 GMT
>What do you mean by "implement Comparable in a generic way"?
implement Comparable == implements Comparable
implement Comparable in a generic way == implements Comparable< xxxx >

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 23 May 2008 13:14 GMT
>> What do you mean by "implement Comparable in a generic way"?
>
> implement Comparable == implements Comparable
> implement Comparable in a generic way == implements Comparable< xxxx >
Aha. So you mean
public class Foo implements Comparable <Foo>
{ ... }
If you leave off the "<Foo>" you get a raw type warning.

Signature
Lew
Roedy Green - 23 May 2008 19:12 GMT
>Aha. So you mean
>
>public class Foo implements Comparable <Foo>
>{ ... }
>
>If you leave off the "<Foo>" you get a raw type warning.
Nearly always I just give a brief summary of what I explain at the
link, in this case
http://mindprod.com/jgloss/comparable.html
and
http://mindprod.com/jgloss/sort.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com