1.1 Working with Arrays
Tiger has a pretty major overhaul of its collection classes, most of which
have to do with generic types and support for the new for/in loop.
Without getting into those details yet, you can get some immediate bang
for your buck by checking out the java.util.Arrays class, which is
chock-full of static utility methods (many of which are new to Tiger).
1.1.1 How do I do that?
The java.util.Arrays class is a set
of static methods that all are useful
for working with arrays. Most of these methods are particularly helpful if
you have an array of numeric primitives, which is what Example 1-1 demonstrates (in varied and mostly useless ways).
Example 1-1. Using the Arrays utility class
The first method to take note of,
package com.oreilly.tiger.ch01;
import java.util.Arrays;
import java.util.List;
public class ArraysTester {
private int[] ar;
public ArraysTester(int numValues) {
ar = new int[numValues];
for (int i=0; i < ar.length; i++) {
ar[i] = (1000 - (300 + i));
}
}
public int[] get( ) {
return ar;
}
public static void main(String[] args) {
ArraysTester tester = new ArraysTester(50);
int[] myArray = tester.get( );
// Compare two arrays
int[] myOtherArray = tester.get().clone( );
if (Arrays.equals(myArray, myOtherArray)) {
System.out.println("The two arrays are equal!");
} else {
System.out.println("The two arrays are not equal!");
}
// Fill up some values
Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue( ));
myArray[30] = 98;
// Print array, as is
System.out.println("Here's the unsorted array...");
System.out.println(Arrays.toString(myArray));
System.out.println( );
// Sort the array
Arrays.sort(myArray);
// print array, sorted
System.out.println("Here's the sorted array...");
System.out.println(Arrays.toString(myArray));
System.out.println( );
// Get the index of a particular value
int index = Arrays.binarySearch(myArray, 98);
System.out.println("98 is located in the array at index " + index);
String[][] ticTacToe = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { {"O", "O", "X"},
{"O", "X", "X"},
{"X", "O", "X"}};
String[][] ticTacToe3 = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println("Boards 1 and 2 are equal.");
} else {
System.out.println("Boards 1 and 2 are not equal.");
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println("Boards 1 and 3 are equal.");
} else {
System.out.println("Boards 1 and 3 are not equal.");
}
}
}
at least for Tiger fans, is toString( ).
This handles the rather annoying task of printing arrays for you. While
this is trivial to write on your own, it's still nice that Sun takes care of it
for you now. Here's some program output, showing the effects of Arrays.
toString( ) on an array:NOTERunning Ant and
supplying a target
of "run-ch01"
automates this.
Another similar, but also new,
run-ch01:
[echo] Running Chapter 1 examples from Tiger: A Developer's Notebook
[echo] Running ArraysTester...
[java] The two arrays are equal!
[java] Here's the unsorted array...
[java][700, 699, 3, 3, 3, 3, 3, 3, 3, 3, 690, 689, 688, 687, 686, 685,
684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 98,
669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655,
654, 653, 652, 651]
[java] Here's the sorted array...
[java] [3, 3, 3, 3, 3, 3, 3, 3, 98, 651, 652, 653, 654, 655, 656, 657,
658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673,
674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688,
689, 690, 699, 700]
[java] 98 is located in the array at index 8
method is deepToString( ). This method
takes in an object array, and prints out its contents, including the contents
of any arrays that it might contain. For example:
Here's the output:
String[][] ticTacToe = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
System.out.println(Arrays.deepToString(ticTacToe));
This starts to really come in handy when you've got three or four levels
[java] [[X, O, O], [O, X, X], [X, O, X]]
of arrays, and don't want to take the time to write your own recursion
printing routines.
Finally, Arrays provides a deepEquals( ) method
that compares multidimensional arrays:
As expected, the first comparison returns false, and the second true:
String[][] ticTacToe = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { {"O", "O", "X"},
{"O", "X", "X"},
{"X", "O", "X"}};
String[][] ticTacToe3 = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}};
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println("Boards 1 and 2 are equal.");
} else {
System.out.println("Boards 1 and 2 are not equal.");
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println("Boards 1 and 3 are equal.");
} else {
System.out.println("Boards 1 and 3 are not equal.");
}
[java] Boards 1 and 2 are not equal.
[java] Boards 1 and 3 are equal.
1.1.2 What About...
...hash codes? Java 101 dictates that every good equals( ) method
should be paired with an
equivalent hashCode( ), and the Arrays class is no exception. Arrays defines both hashCode( ) and deepHashCode( ) methods for just this purpose. I'll leave it to you to play with these, but
they are self-explanatory:
int hashCode = Arrays.deepHashCode(ticTacToe);