Working with arrays : java.util.Arrays class

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.

also read:

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.

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.

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.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*