English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Compare two Java int arrays in Java

To compare two Java short arrays, use the Arrays.equals() method.

Let's first declare and initialize some int arrays.

int[] arr1 = new int[] { 33, 66, 12, 56, 99 };
int[] arr2 = new int[] { 33, 66, 12, 56, 99 };
int[] arr3 = new int[] { 33, 66, 15, 56, 90 });

Now let's compare the two arrays above.

Arrays.equals(arr1, arr2));

Similarly, use it for other arrays and compare

Example

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      int[] arr1 = new int[] { 33, 66, 12, 56, 99 };
      int[] arr2 = new int[] { 33, 66, 12, 56, 99 };
      int[] arr3 = new int[] { 33, 66, 15, 56, 90 });
      System.out.println(Arrays.equals(arr1, arr2));
      System.out.println(Arrays.equals(arr2, arr3));
      System.out.println(Arrays.equals(arr3, arr1));
   }
}

Output Result

true
false
false