I like generics, just think the syntax is ugly and hard to read.
What can ya do...
Thanks Wayne, that's a few pages to read there, will do, will do. Can
you translate the above mentioned 5.0 specification of
binarySearch(List<? extends Comparable<? super T>> list, T key)
to a couple of english sentances?
Like, looking for something of type T.... and looking in a list that
has.... what in it? not just things of type T.... The things in the
list are....
They are question marks.... ok... and that is an interface because
Comparable is an interface.... So oh darn.... why is The comparabale
got <? super T>.... I get lost...
Wayne - 25 May 2005 06:26 GMT
> Thanks Wayne, that's a few pages to read there, will do, will do. Can
> you translate the above mentioned 5.0 specification of
[quoted text clipped - 10 lines]
> Comparable is an interface.... So oh darn.... why is The comparabale
> got <? super T>.... I get lost...
Well I'm hardly an expert but I'll try to explain. (If I get it
wrong someone will yell at me and then you'll know the right
answer. :-)
Short answer:
BinarySearch method takes a (sorted) list of Comparable items,
and an item to search for.
Less short answer:
The questionmark is a kind of wild-card. This is needed because
List<Object> is *not* a supertype of List<String>, despite that it
looks like it should be. From the Generics PDF:
List<String> ls = new ArrayList<String>();
List<Object> lo = ls; // error
The second line won't compile! You need to use a "List of any type":
List<?> lo = ls;
For binarySearch, List<?> as an argument would be too general. You
only want lists of Comparables (or types that extend Comparables).
Apparently to avoid introducing new keywords in Java, they came
up with this ugly syntax:
List<? extends Comparable>
This means a list of anything that implements or extends Comparable.
So given:
class Foo {}
class Bar extends Foo {}
Then I can pass a list of Bars to a method expecting: List<? extends Foo>
(Technically this is called a "bounded wildcard".)
The syntax:
List<? super Bar>
is similar. It defines a list of objects of type Bar, or
any superclass of Bar. (This is called a "lower bound".) Examine
the following code and note the lines marked as giving compiler
errors, hopefully they should make sense to you:
==================
import java.util.*;
class Foo implements Comparable<Foo>
{
public int compareTo ( Foo obj ) { return 0; }
}
class Bar extends Foo {}
public class GenericsTest
{
private static Foo foo = new Foo();
private static Bar bar = new Bar();
public static void main ( String [] args )
{
baseTest();
extendsTest();
superTest();
}
static void baseTest ()
{
List<Foo> fooList = new ArrayList<Foo>();
List<Bar> barList = new ArrayList<Bar>();
fooList.add( foo );
fooList.add( bar );
// barList.add( foo ); // error
barList.add( bar );
Collections.binarySearch( fooList, foo);
Collections.binarySearch( fooList, bar);
Collections.binarySearch( barList, foo);
Collections.binarySearch( barList, bar);
}
static void extendsTest ()
{
List<? extends Foo> fooList = new ArrayList<Foo>();
List<? extends Bar> barList = new ArrayList<Bar>();
// fooList.add( foo ); // error
// fooList.add( bar ); // error
// barList.add( foo ); // error
// barList.add( bar ); // error
}
static void superTest ()
{
List<? super Foo> fooList = new ArrayList<Foo>();
List<? super Bar> barList = new ArrayList<Bar>();
fooList.add( foo );
fooList.add( bar );
// barList.add( foo ); // error
barList.add( bar );
}
}
==============
Final issue:
binarySearch ( List<? extends Comparable>, XXX key )
What should "XXX" (the type of parameter "key") be? The answer is that
it should be the same type as the items in the list. To say I have a
method that takes a list of any type and an object of that same type
requires the use of "generic methods". The full declaration for
binarySearch in Java5 is:
public static <T> int binarySearch (
List<? extends Comparable<? super T>> list,
T key
)
The "<T>" says this is a generic method, where "T" is some type whenever
it is seen in the declaration. (The same type all the time.)
Now we can read this: java.utils.Collections.binarySearch is a public
static method that takes two arguments. The first is a List of Comparable
items of some type T (or any supertype of T), and a "key" of type T.
Hope this helps a bit!
-Wayne
opalpa@gmail.com - 25 May 2005 22:36 GMT
Thank you. That's many bits worth of help. Now I comprehend.
John Maline - 25 May 2005 19:52 GMT
There was a good article at http://onjava.com covering exactly this kind
of stuff.
http://www.onjava.com/pub/a/onjava/excerpt/javaian5_chap04/index1.html
> Thanks Wayne, that's a few pages to read there, will do, will do. Can
> you translate the above mentioned 5.0 specification of
[quoted text clipped - 10 lines]
> Comparable is an interface.... So oh darn.... why is The comparabale
> got <? super T>.... I get lost...