Finding an Element in a Sorted List

// Create a list with an ordered list of strings List sortedList = new LinkedList(); sortedList.addAll(Arrays.asList(new String[]{"ant", "bat", "cat", "dog"})); // Search for the word "cat" int index = Collections.binarySearch(sortedList, "cat"); // 2 // Search for a non-existent element index = Collections.binarySearch(sortedList, "cow"); // -4
A negative return value indicates that the element is not in the list. However, the actual return value can be used to determine where that non-existent element should be inserted in the list if that were desired; see Inserting an Element into a Sorted List.

Comments

7 Jan 2010 - 10:47pm by vidhya (not verified)

Instead of enabling properties via -D, is there any option to download automatically

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.