Compare Two Java int Arrays in Java

Java 8Object Oriented ProgrammingProgramming

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

Let us 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 us compare any two of the above arrays.

Arrays.equals(arr1, arr2));

In the same way, work it for other arrays and compare them

Example

 Live Demo

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

true
false
false
raja
Published on 13-Nov-2018 09:16:29
Advertisements