• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Working with arrays : java.util.Arrays class

February 19, 2009 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

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.

Category: JavaTag: Core Java

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.

Previous Post: « How to use Enum in Switch?
Next Post: Volatile keyword in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact