aspose file tools*
The moose likes Beginning Java and the fly likes List<? extends Comparable<? super T>> list, T key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List<? extends Comparable<? super T>> list, T key" Watch "List<? extends Comparable<? super T>> list, T key" New topic
Author

List<? extends Comparable<? super T>> list, T key

Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Hello guys:

Basically, I was tasked to write an application, when the users key in the admin number (A String), a particular method is supposed to search the ArrayList and return to tell the user if that particular Student record was found in the ArrayList. I have an ArrayList called studArr, I tested it, and I confirmed that there are student objects inside the arrayList.

The question want me to use the binarySearch method in the Java API to do the searching but I don't understand how it works.



Where searchInput is the Admin No entered by the user. studArr is the ArrayList. I don't know how to use the binarySearch method, what is extendsComparable super T, what am I supposed to put inside? Are the arguments correct?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
For a binary search to work, the List must be sorted before use, and the searching must use the same ordering as the sorting.

What <? extends Comparable<? super T>> means is that whatever type you have in the List must be Comparable, which means it implements the Comparable interface. But Comparable takes a type itself, so it must be comparable to something. ~so we call that something T. So T also implements Comparable, but it might do so directly, or indirectly (via a superclass), so the bit about ? super means it can be something, we know not what, which is a superclass of T. Remember for the purposes of this exercise, you should think that T is a superclass of itself.

So your List might contain a T. But what if it contains a subclass of T. All subclasses of T will be Comparable to T as well. So you can say it is an unknown type, but it extends T So you start with

Comparable<? super T> and when you add subclasses of T as well, you get

<? extends Comparable<? super T>>
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Campbell Ritchie wrote:For a binary search to work, the List must be sorted before use, and the searching must use the same ordering as the sorting.

What <? extends Comparable<? super T>> means is that whatever type you have in the List must be Comparable, which means it implements the Comparable interface. But Comparable takes a type itself, so it must be comparable to something. ~so we call that something T. So T also implements Comparable, but it might do so directly, or indirectly (via a superclass), so the bit about ? super means it can be something, we know not what, which is a superclass of T. Remember for the purposes of this exercise, you should think that T is a superclass of itself.

So your List might contain a T. But what if it contains a subclass of T. All subclasses of T will be Comparable to T as well. So you can say it is an unknown type, but it extends T So you start with

Comparable<? super T> and when you add subclasses of T as well, you get

<? extends Comparable<? super T>>


Thanks for your reply. I've already sorted the list before hand so no worries. (: I'm not sure what it mean, but I have a class named Student that implements Comparable.

I tried doing this but it doesn't work.



Where

studArr is the List
Student is the class that implements Comparable

Is it because of my understanding?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
Please don't use red writing; many people find it difficult to read.

If Student implements Comparable<Student> then you simply pass the List as an argument to Collections#sort().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
. . . and then you pass the List and the sought index to Collections#binarySearch() . . . which you would appear to have done.

What goes wrong? I can't see your error yet.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
I think I have seen the error. It should be Collections.binarySearch(studArr, searchInput) You don't write <Student>
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Campbell Ritchie wrote: . . . and then you pass the List and the sought index to Collections#binarySearch() . . . which you would appear to have done.

What goes wrong? I can't see your error yet.


Here's the screenshot of the error:

http://i53.tinypic.com/3535y5v.jpg

If I remove the <Student> the error shows the argument is not applicable etc. Is my logic wrong or anything?
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Campbell Ritchie wrote:I think I have seen the error. It should be Collections.binarySearch(studArr, searchInput) You don't write <Student>


I tried that before hand before posting on the forum:

http://i52.tinypic.com/bex4t5.jpg
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 40
Aces Kok Ben, you are trying to search a List of Student by using a String.

I'm not sure what it mean, but I have a class named Student that implements Comparable.


Have you implemented it correctly or just implemented it so that it compiles? Comparable is used to compare two instances of a the same class. It must decide if they are equal, and if not which one is the larger.




So what you must do is search your list with an object of type Student, not of type String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
Aces Kok Ben wrote: . . .
Here's the screenshot of the error:

http://i53.tinypic.com/3535y5v.jpg . . .
Why didn't you tell us the text of the error first?
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Eoin Brosnan wrote:Aces Kok Ben, you are trying to search a List of Student by using a String.

I'm not sure what it mean, but I have a class named Student that implements Comparable.


Have you implemented it correctly or just implemented it so that it compiles? Comparable is used to compare two instances of a the same class. It must decide if they are equal, and if not which one is the larger.




So what you must do is search your list with an object of type Student, not of type String.


Ok thanks. I managed to fix the error myself. You're right, the second argument should be an Student object, not String.
 
Consider Paul's rocket mass heater.
 
subject: List<? extends Comparable<? super T>> list, T key