Learnitweb

Comparing Arrays for equality in Java

Two arrays are said to be equal if the arrays have equal number of elements and all corresponding pairs of elements in two arrays are equal.

Method 1: Iterative method

Following is the logic:

  • Compare the length of two arrays. If the length of two arrays is not equal then return false.
  • If the length of two arrays is equal, compare the corresponding elements in both arrays.
  • If all the corresponding elements are equal then the arrays are equal else not equal.

This method is not not recommended for large sized array.

public class ArrayComparisonExample {
	public static void main(String[] args) {
		int[] arrayOne = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		int[] arrayTwo = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		boolean result = compareArrays(arrayOne, arrayTwo);

		if (result == true) {
			System.out.println("Two arrays are equal");
		} else {
			System.out.println("Two arrays are not equal");
		}
	}

	public static boolean compareArrays(int[] arrayOne, int[] arrayTwo) {
		boolean isEqual = true;

		if (arrayOne.length == arrayTwo.length) {
			//compare elements of arrays
			for (int i = 0; i < arrayOne.length; i++) {
				if (arrayOne[i] != arrayTwo[i]) {
					isEqual = false;
				}
			}
		} else {
			isEqual = false; //If length of arrays is not equal, return false
		}

		return isEqual;
	}
}

Method 2: Using Arrays.equals()

Arrays.equals() method returns true if the two specified arrays are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

import java.util.Arrays;

public class ArrayComparisonExample {
	public static void main(String[] args) {
		int[] arrayOne = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		int[] arrayTwo = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		boolean result = compareArrays(arrayOne, arrayTwo);

		if (result == true) {
			System.out.println("Two arrays are equal");
		} else {
			System.out.println("Two arrays are not equal");
		}
	}

	public static boolean compareArrays(int[] arrayOne, int[] arrayTwo) {
		return Arrays.equals(arrayOne, arrayTwo);

	}
}

deepEquals() method – checking multi-dimensional arrays

If you want to check for multi-dimensional array, use deepEquals() method of Arrays. Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.

import java.util.Arrays;

public class ArrayComparisonExample {
	public static void main(String[] args) {
		String[][] s1 = { { "1", "2", "3" }, { "4", "5", "6" } };

		String[][] s2 = { { "1", "2", "3" }, { "4", "5", "6" } };

		if (compareArrays(s1, s2) == true) {
			System.out.println("Arrays are equal");
		} else {
			System.out.println("Arrays are not equal");
		}

	}

	public static boolean compareArrays(String[][] s1, String[][] s2) {

		return Arrays.deepEquals(s1, s2);

	}
}