| Author |
please explain Generics compilation error
|
raj malhotra
Ranch Hand
Joined: Feb 22, 2007
Posts: 285
|
|
I have written the following code: I am getting compilation error:Bound mismatch: The generic method sort(List<T> ) of type Collections is not applicable for the arguments (ArrayList<Dvd> ) . The inferred type Dvd is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> at Generics1.main. can anyone explain what Comparable<? super T> means Thanks Raj [ May 29, 2007: Message edited by: raj malhotra ]
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Your DVD class must implement Comparable interface so that sort() method could sort the object of DVD whose references are staying in the ArrayList. With Regards,
|
cmbhatt
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
can anyone explain what Comparable<? super T> means
In simple wordings it does mean that it is not must that class T implements the Comparable interface. If super class of that implements the Comparable it will also do. In your case you experiment with what I say: 1- First try by making your Dvd class implement Comparable interface. 2- After that you make a new class and extend the Dvd class from that. Parent class of Dvd class should implement the Comparable this time so that you could test the meaning of Comparable<? super T> Regards, cmbhatt
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Following on from what Chandra wrote: class Dvd can implement interface Comparable<Dvd> simply by having a suitable int compareTo(Dvd dvd) method defined. [ May 29, 2007: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
raj malhotra
Ranch Hand
Joined: Feb 22, 2007
Posts: 285
|
|
Got it.Any class ,which implements Comparable interface or whose super class implements it,can be used to sort using Collections.sort(T o) Thanks Chandra & Barry [ May 29, 2007: Message edited by: raj malhotra ]
|
 |
 |
|
|
subject: please explain Generics compilation error
|
|
|