java.util.Arrays
helpers
final String[] names = {"Eve", "Aaron", "Paul", "Mandy"};
System.out.println(" toString: " + Arrays.toString(names));
Arrays.sort(names);
System.out.println("sort|toString: " + Arrays.toString(names));
Result:
toString: [Eve, Aaron, Paul, Mandy] sort|toString: [Aaron, Eve, Mandy, Paul]
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"));
Result:
sort|find(Mand): -3 sort|find(Mandy): 2 sort|find(Mandyer): -4
No. 144
Understanding search results
Q: |
In Figure 438, “ sort|find(Mand): -3 sort|find(Mandy): 2 sort|find(Mandyer): -4
TipRead |
||||||||||
A: |
The input array reads
|
final String[] names =
{"Eve", "Aaron", "Paul", "Mandy"};
System.out.println("toString: " +
Arrays.toString(names));
Arrays.fill(names, "N.N");
System.out.println("toString: " +
Arrays.toString(names)); |
toString: [Eve, Aaron, Paul, Mandy] toString: [N.N, N.N, N.N, N.N] |
final String[] names = {"Eve", "Aaron", "Paul", "Mandy"};
final String[] lastTwoNames = Arrays.copyOfRange(names, 2, 6);
System.out.println("toString: " + Arrays.toString(lastTwoNames));
Result:
toString: [Paul, Mandy, null, null]
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));
Result:
l1.equals(l2):true l1.equals(l3):false