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.

Lawrence Louie

-- 
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to