I believe this problem is arising because in order to use
Colelctions.binarySearch() you must first sort your list.
Try modifying as below and compare the results.
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Question {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method
// stub
List ll = new
LinkedList();
ll.add(new Integer (5));
ll.add(new Integer (8));
ll.add(new Integer (3));
ll.add(new Integer (4));
int index5 = Collections.binarySearch(ll, new Integer(5));
int index8 = Collections.binarySearch(ll, new Integer(8));
int index3 = Collections.binarySearch(ll, new Integer(3));
int index4 = Collections.binarySearch(ll, new Integer(4));
System.out.println(ll);
System.out.println("5 is in index " + index5);
System.out.println("8 is in index " + index8);
System.out.println("3 is in index " + index3);
System.out.println("4 is in index " + index4);
// Sort the linked list
Collections.sort(ll);
index5 = Collections.binarySearch(ll, new Integer(5));
index8 = Collections.binarySearch(ll, new Integer(8));
index3 = Collections.binarySearch(ll, new Integer(3));
index4 = Collections.binarySearch(ll, new Integer(4));
System.out.println(ll);
System.out.println("5 is in index " + index5);
System.out.println("8 is in index " + index8);
System.out.println("3 is in index " + index3);
System.out.println("4 is in index " + index4);
}
}
Output now is:
-------------------------------
[5, 8, 3, 4]
5 is in index 0
8 is in index 1
3 is in index -1
4 is in index -1
[3, 4, 5, 8]
5 is in index 2
8 is in index 3
3 is in index 0
4 is in index 1
On Thu, Feb 17, 2011 at 1:17 AM, Michèle Garoche <[email protected]> wrote:
>
>
> On Feb 16, 11:49 pm, Lawrence Louie <[email protected]> wrote:
> > import java.util.Collections;
> > import java.util.Iterator;
> > import java.util.LinkedList;
> > import java.util.List;
> >
> > public class Question {
> >
> > /**
> > * @param args
> > */
> > public static void main(String[] args) {
> > // TODO Auto-generated method stub
> >
> > List ll = new LinkedList();
> > ll.add(new Integer (5));
> > ll.add(new Integer (8));
> > ll.add(new Integer (3));
> > ll.add(new Integer (4));
> >
> > int index5 = Collections.binarySearch(ll, new
> Integer(5));
> > int index8 = Collections.binarySearch(ll, new
> Integer(8));
> > int index3 = Collections.binarySearch(ll, new
> Integer(3));
> > int index4 = Collections.binarySearch(ll, new
> Integer(4));
> > System.out.println(ll);
> > System.out.println("5 is in index " + index5);
> > System.out.println("8 is in index " + index8);
> > System.out.println("3 is in index " + index3);
> > System.out.println("4 is in index " + index4);
> >
> > }
> >
> > }
> >
> > Output is:
> > -------------------------------
> > [5, 8, 3, 4]
> > 5 is in index 0
> > 8 is in index 1
> > 3 is in index -1
> > 4 is in index -1
> >
> > Instead of using the LinkedList, I have also tried it with ArrayList,
> > same result.
> >
> > Questions: why is 3 or 4 returns -1 in the index? Thx.
> You should sort the collection, that is the LinkedList, before
> applying binarySearch on it, because binarySearch operates on a sorted
> list, and adding multiple objects to a LinkedList does not preserve
> the natural order.
>
> Just add:
> Collections.sort(ll);
> before the first line of int indexx = Collections.binarySearch(...);
>
> Then you should get the following output:
>
> [3, 4, 5, 8]
> 5 is in index 2
> 8 is in index 3
> 3 is in index 0
> 4 is in index 1
>
> Michèle Garoche
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en