### Java Program to Check the Equality of Two Arrays

In this post, we will write a Java program to check an equality of two Arrays.
In this post, we will see three ways we can write logic to check an equality of two Arrays.

• Using an iterative method
• Using Arrays.equals(array1, array2) Method
• Write equals Generic Method

## Using an iterative method

```public class CheckTwoArraysAreEqual {

public static void main(final String[] args) {

final int[] array1 = { 1, 2, 3, 4, 5 };
final int[] array2 = { 1, 2, 3, 4, 5 };

final boolean intCheck = equals(array1, array2);
System.out.println("Two Integers are Equal :: " + intCheck);
}

public static boolean equals(final int[] a, final int[] a2) {
if (a == a2)
return true;
if (a == null || a2 == null)
return false;

final int length = a.length;
if (a2.length != length)
return false;

for (int i = 0; i < length; i++)
if (a[i] != a2[i])
return false;

return true;
}
}```
Output:
``````Two Integers are Equal :: true
``````

## Using Arrays.equals(array1, array2) Method

```import java.util.Arrays;

public class CheckTwoArraysAreEqual {

public static void main(final String[] args) {

final int[] array1 = { 1, 2, 3, 4, 5 };
final int[] array2 = { 1, 2, 3, 4, 5 };

final boolean intCheck = Arrays.equals(array1, array2);
System.out.println("Two Integers are Equal :: " + intCheck);
}
}```
Output:
``````Two Integers are Equal :: true
``````

## Write equals Generic Method

```public class CheckTwoArraysAreEqual {

public static void main(final String[] args) {

final String[] s1 =  { "Apple", "banana", "pinapale" };

final String[] s2 =  { "Apple", "banana", "pinapale" };

final Integer[] array1 = { 1, 2, 3, 4, 5 };
final Integer[] array2 = { 1, 2, 3, 4, 5 };

final boolean strCheck = equals(s1, s2);
System.out.println("Two Strings are Equal :: " + strCheck);
final boolean intCheck = equals(array1, array2);
System.out.println("Two Integers are Equal :: " + intCheck);
}

public static boolean equals(final Object[] a, final Object[] a2) {
if (a == a2)
return true;
if (a == null || a2 == null)
return false;

final int length = a.length;
if (a2.length != length)
return false;

for (int i = 0; i < length; i++) {
final Object o1 = a[i];
final Object o2 = a2[i];
if (!(o1 == null ? o2 == null : o1.equals(o2)))
return false;
}

return true;
}
}```
Output:
``````Two Strings are Equal :: true
Two Integers are Equal :: true``````