java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to compare if two array is equal?

Using Arrays.equals() methods we can compare if two arrays are equals. Two arrays are considered to be equal if their length, each element in the array are equal and in the same order to one another.

The code below give you an example:

import java.util.Arrays;

public class CompareArrayExample
{
    public static void main(String[] args)
    {
        String[] abc = {"Kode", "Java", "Dot", "Org"};
        String[] xyz = {"Kode", "Java", "Dot", "Org"};
        String[] java = {"Java", "Dot", "Com"};

        System.out.println(Arrays.equals(abc, xyz));
        System.out.println(Arrays.equals(abc, java));
    }
}

The Arrays.equal() can be used to compare array of any primitive data type and array of Object. If you run this example you will have a result as follow:

true
false