Compare Two Java int Arrays in Java



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
Updated on: 2020-06-26T10:53:52+05:30

567 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements