write a java program to check two arrays are equal or not ?

This tutorial explains how to check two arrays are equal or not with help of an example.Arrays.equals() method is used to check two arrays are equal or not .

Steps to write program:

1) Create and intialize two arrays.

2) Compare two arrays by using Arrays.equals() method which takes tw arrays and returns boolean value if they are true else it returns false.

Method signature:

static boolean equals(boolean[] a1,boolean[] a2)
static boolean equals(byte[] a1,byte[] a2)
static boolean equals(char[] a1,char[] a2)
static boolean equals(double[] a1,double[] a2)
static boolean equals(float[] a1,float[] a2)
static boolean equals(int[] a1,int[] a2)
static boolean equals(long[] a1,long[] a2)
static boolean equals(Object[] a1,Object[] a2)
static boolean equals(short[] a1,short[] a2)

Java example to check two arrays are equal or not

import java.util.Arrays;

public class ArraysEquals {

	public static void main(String[] args) {

		Integer array1[]={1,2,3,4,5};
		Integer array2[]={2,1,3,4,5};
		System.out.println(Arrays.equals(array1 ,array2));

	}
}

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *