How to compare two arrays in Java if they are Equal or Not

How to compare two arrays in java if they are equal or not using 2 methods with code example.

  • By Comparing each corresponding element of both the arrays.
  • Using Arrays.equals() method.

Two arrays will be called equal if both the arrays have same corresponding elements and same number of elements.

Comparing each element of both the arrays for their equality in Java

This is the very simple method in which we will follow below steps to compare array elements and check if both arrays are equal or not.

Steps:

  1. Create 2 arrays with elements.
  2. Check the length of both arrays and compare it. If they are not equal, then arrays are not equal and no need to process further.
  3. if both the arrays length is equal, then retrieve each corresponding element from both array by traversing within a loop and compare them till last element. If all are equal, then array will be equal or if any of the elements are not equal then arrays will not be equal.

Java Code:


/*-----------------------------------------------------
 * Java program to check if two arrays are equal or not
 */
public class Sample{

	public static void main(String[] args) {

		// Set boolean as true initially
		boolean isArrayEqual = true;//

		// Declare two Arrays
		int[] arr1 = { 1, 2, 3, 4, 4, 5 };
		int[] arr2 = { 1, 2, 3, 4, 4, 5, 6 };

		// First, Check if length of both array are equal
		// If not equal...no need to traverse array 
		//and make Comparison as they cannot be equal
		if (arr1.length == arr2.length) {

			// Traverse both array and compare
			//each element
			for (int i = 0; i < arr2.length; i++) {

				// set true if each corresponding
				//elements of arrays are equal
				if (arr2[i] != arr1[i]) {

					isArrayEqual = false;
				}
			}
		} else {

			isArrayEqual = false;
		}

		// print the result if both arrays are equal or not
		if (isArrayEqual) {
			System.out.println("Both arrays are equal");
		} else {
			System.out.println(" Both arrays are not equals");
		}

	}

}

Input:

int[] arr1 = { 1, 2, 3, 4, 4, 5 };
int[] arr2 = { 1, 2, 3, 4, 4, 5 };

Output:
Both arrays are equal

Input:
int[] arr1 = { 1, 2, 3, 4, 4, 5 };
int[] arr2 = { 1, 2, 3, 4, 4, 5 ,6};

Output:
Both arrays are not equal

How to compare two arrays in java using Arrays.equals() method

We can also compare two arrays for their equality using Arrays.equals method of Array class that is present in java.util.Arrays class.The method takes two arguments arr1 and arr2. By comparing both the array it will return a Boolean value true or false if they are equal or not respectively.

Java Code:

/*-----------------------------------------------------
 * Java program to compare two arrays for
 * equality using Arrays.equals method.
 */
import java.util.Arrays;

public class Sample{

	public static void main(String[] args) {

		// Set boolean as true initially
		boolean isArrayEqual = true;//

		// Declare two Arrays
		int[] arr1 = { 1, 2, 3, 4, 4, 5 };
		int[] arr2 = { 1, 2, 3, 4, 4, 5 };

		//
		isArrayEqual = Arrays.equals(arr1, arr2);

		// Print the result if both arrays are equal or not
		if (isArrayEqual) {
			System.out.println("Both arrays are equal");
		} else {
			System.out.println(" Both arrays are not equal");
		}

	}

}


Leave a Comment