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 431. Arrays.binarySearch(...) Slide presentation
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. 149

Understanding search results


In Figure 431, “Arrays.binarySearch(...) the result reads:

sort|find(Mand): -3
sort|find(Mandy): 2
sort|find(Mandyer): -4
  1. What does the positive value 2 in return to "Mandy" indicate?

  2. Why do we get negative values -3 and -4 for "Mand" and "Mandyer" respectively?

  3. What is the exact meaning of -3 and -4 besides being just negative?


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

Array element Index
"Aaron" 0
"Eve" 1
"Mandy" 2
"Paul" 3
  1. A positive value indicates an exact match: The string "Mandy" does exist within the array at index position 2.

  2. A negative value indicates a value not being found: Both "Mand" and "Mandyer" do not exist in the array

  3. The exact value of a negative integer being returned defines the hypothetical insertion position keeping the resulting array sorted. Regarding "Mandyer" the insertion index would be 3 squeezing "Paul" one step to the right:

       {"Aaron", "Eva", "Mandy", "Mandyer", "Paul"} 
    /* Index 0      1        2          3      4 /*

    The observed return value of -4 when searching for "Mandyer" thus corresponds to insertion index 3. Hence we have the following rule:

    Negate the return value and subsequently subtract 1.

    Ratio: 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 an existing value"A" at its leftmost start position.

Figure 432. Arrays.fill(...) Slide presentation
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 433. Arrays.copyOfRange(...) Slide presentation
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 434. Arrays.equals(...) Slide presentation
final String[]
  l1 = {new String("Eve"), new String("Aaron"), 
        new String("Paul"), new String("Mandy")},

  l2 = {new String("Eve"), new String("Aaron"), 
        new String("Paul"), new String("Mandy")},

  l3 = {new String("Eve"), new String("Aaron"), 
        new String("Paul"), new String("Mobile")};

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