final String[] names = {"Eve", "Aaron", "Paul", "Mandy"};

System.out.println("     toString: " + Arrays.toString(names));


System.out.println("sort|toString: " + Arrays.toString(names));


     toString: [Eve, Aaron, Paul, Mandy]
sort|toString: [Aaron, Eve, Mandy, Paul]

Figure 396. Arrays.binarySearch(...) Slide presentation Create comment in forum
final String[] names = {"Aaron", "Eve", "Mandy",  "Paul"};

// Precondition: Array must be ordered!
...println("sort|find(Mand): " + Arrays.binarySearch(names, "Mand"));
...println("sort|find(Mandy): " + Arrays.binarySearch(names, "Mandy"));
...println("sort|find(Mandyer): " + Arrays.binarySearch(names, "Mandyer"));


sort|find(Mand): -3
sort|find(Mandy): 2
sort|find(Mandyer): -4

exercise No. 132

Understanding search results Create comment in forum


Read the Javadoc of Arrays.binarySearch(...)and explain the three integer search result values in Figure 396, “Arrays.binarySearch(...).

In particular give a detailed account explaining the two negative values -3 and -4.


The input array reads {"Aaron", "Eva", "Mandy", "Paul"}.

Array element Index
"Aaron" 0
"Eve" 1
"Mandy" 2
"Paul" 3

A positive return value conveys:

  1. The value is present within the array.

  2. The return value is equal to the array element's index.

Searching for "Mandy" thus returns the index value 2 corresponding to the third array element.

Negative return values actually also provide two types of information:

  1. The value is not present in the given array.

  2. The position where the element would be inserted to become part of an ordered list.

In the given example hypothetically inserting the string "Mand" retaining a sorted list results in:

{"Aaron", "Eva", "Mand", "Mandy", "Paul"}

This insertion point's index is 2 corresponding to the third array element. According to the binarySearch(...) method's documentation this corresponds to a return value of -2 - 1 == -3.

The observed return value of -4 when searching for "Mandyer" corresponds to insertion index 3:

{"Aaron", "Eva", "Mandy", "Mandyer", "Paul"}

Subtracting -1 is being required for conveying the «not in list» information under all circumstances. Otherwise not finding and hypothetically inserting e.g. "A" at the list's beginning would result in 0. This could not be disambiguated from an array featuring "A" at its front position.

Figure 397. Arrays.fill(...) Slide presentation Create comment in forum
final String[] names = 
  {"Eve", "Aaron", "Paul", "Mandy"};

System.out.println("toString: " +

Arrays.fill(names, "N.N");

System.out.println("toString: " + 
toString: [Eve, Aaron, Paul, Mandy]
toString: [N.N, N.N, N.N, N.N]

Figure 398. Arrays.copyOfRange(...) Slide presentation Create comment in forum
final String[] names = {"Eve", "Aaron", "Paul", "Mandy"};

final String[] lastTwoNames = Arrays.copyOfRange(names, 2, 6);

System.out.println("toString: " + Arrays.toString(lastTwoNames));


toString: [Paul, Mandy, null, null]

Figure 399. Arrays.equals(...) Slide presentation Create comment in forum
final String[]
  l1 = {"Eve", "Aaron", "Paul", "Mandy"},
  l2 = {"Eve", "Aaron", "Paul", "Mandy"},
  l3 = {"Eve", "Aaron", "Paul", "Mobile"};

System.out.println("l1.equals(l2):" + Arrays.equals(l1, l2));
System.out.println("l1.equals(l3):" + Arrays.equals(l1, l3));