Working with arrays : java.util.Arrays class

February 19, 2009

Java

The java.util.Arrays class is basically a set of static methods that are all useful for working with arrays.
The Arrays class contains various methods for manipulating arrays (such as sorting and searching). In addition to
that, it has got many utility methods for using with arrays such as a method for viewing arrays as lists and methods for
printing the contents of an array, whatever be the dimension of the array.

The following example code snippet will give you a heads up into how some of the more commonly used methods of the
Arrays class is used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Arrays;
 
public class ArraysMethodTester {
	private char[] myArray = {'b', 'e', 'd', 'h','j',
			'a', 'c', 'f', 'g', 'i'};
 
	public char[] getArray(){
		return myArray;
	}
 
	public static void main(String[] args) {
		ArraysMethodTester test = new ArraysMethodTester();
		char[] firstArray = test.getArray();
		char[] secondArray = test.getArray().clone();
 
		// Compare two arrays
		if (Arrays.equals(firstArray, secondArray)) {
			System.out.println("The two arrays are equal!");
		} else {
			System.out.println("The two arrays are not equal!");
		}
 
		firstArray[8] = 'k';
 
		// Print array
		System.out.println("Unsorted array...");
		System.out.println(Arrays.toString(firstArray));
		System.out.println();
 
		// Sort the array
		Arrays.sort(firstArray);
 
		// print sorted array
		System.out.println("Sorted array...");
		System.out.println(Arrays.toString(firstArray));
		System.out.println();
 
		// Get the index of a particular value
		int index = Arrays.binarySearch(firstArray, 'k');
		System.out.println("k is located in the array at index " + index);
		System.out.println();
 
		String[][] ticTacToe = {
				{ "#", "O", "O" },
				{ "O", "#", "#" },
				{ "#", "O", "#" } };
 
		System.out.println(Arrays.deepToString(ticTacToe));
		System.out.println();
 
		String[][] ticTacToe2 = {
				{ "O", "O", "#" },
				{ "O", "#", "#" },
				{ "#", "O", "#" } };
 
		String[][] ticTacToe3 = {
				{ "#", "O", "O" },
				{ "O", "#", "#" },
				{ "#", "O", "#" } };
 
		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.");
		}
	}
}

Given below is the output of the above program.

1
2
3
4
5
6
7
8
9
10
11
12
13
The two arrays are equal!
Unsorted array...
[b, e, d, h, j, a, c, f, k, i]
 
Sorted array...
[a, b, c, d, e, f, h, i, j, k]
 
k is located in the array at index 9
 
[[#, O, O], [O, #, #], [#, O, #]]
 
Boards 1 and 2 are not equal.
Boards 1 and 3 are equal.

The first static method to take note of here is the toString() method which takes care
of the trivial task of printing the contents of an Array for which previously we would
have had to loop through the array.

Another similar but new 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.
Basically, it returns a string representation of the “deep contents” of the specified array.
If the array contains other arrays as elements, the string representation contains their
contents and so on. This method is designed for converting multidimensional arrays to strings.

Finally, the Arrays class also provides a deepEqual() method, that is used to compare
multi-dimensional array. It returns true if the two specified arrays are deeply equal to one another.
This method is appropriate for use with nested arrays of arbitrary depth.

email

Comments

comments