Array Java Programs

Compare two arrays are equal or not in size

In this tutorial you will learn how to write a Java program to compare two arrays and check if they are the same size or not.

Below is the procedure we will follow to achieve our goal:

  1. Our approach is to check two given arrays whether they are equal in size or not by first finding the size of both arrays and then comparing the size.
  2. If the size is same, then “size of both arrays are same” will be printed and if the size is not same, then “size of arrays are not same” will be printed.

How will our program behave?

As we have already seen above, our logic checks whether the size of two given arrays are equal or not.In this program, we already have two arrays. After running the program, the output will be printed according to the array size after the calculation.

Python Program to check the size of given two arrays are equal or not?

arr1=[1,2,3,4,5]
arr2=[1,3,4,5,7]
if len(arr1) == len(arr2):
    print("array is equal")
else:
    print("array is not equal") 

In Java, Check the size of given two arrays are equal or not?

public class Main {
	public static void main(String[] args) {
		int sum = 0;
		int arr1[] = { 1, 2, 3, 4, 5 };
		int arr2[] = { 2, 3, 1, 0, 5 };
		if (arr1.length == arr2.length) {
			System.out.println("Array is equal");
		} else {
			System.out.println("Array is not equal");
		}
	}
}

Output

Array is equal

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs