Given input integer arrays arr1 and arr2, check if the arrays are equal. Arrays are said to be equal if length of both arrays is same and the element at each index in first array is equal to element at same index in the second array.
| Input Parameters |
Expected outputs |
arr1={1,2,3,4}
arr2={1,2,3,4}
|
The arrays are equal !
|
arr1={7,8}
arr2={9,8}
|
The arrays are *not* equal !
|
arr1={10,20}
arr2={30,40,50}
|
The arrays are *not* equal !
|
| The java method should accept following input parameters: arr1 (int array), arr2 (int array).
|
| Check lengths of the two arrays, if they are not same, return false.
|
| Iterate through arr1 using a for loop using idx as loop variable:
If arr1[idx] != arr2[idx], return false.
|
| If the above loop completes without returning false, then it means elements at all indexes are equal, so return true.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayCheckEquality {
public boolean arrayCheckEquality(int[] arr1, int[] arr2) throws Exception{
if (arr1.length != arr2.length) return false;
for (int idx=0; idx< arr1.length; idx++) {
if (arr1[idx] != arr2[idx]) return false;
}
return true;
}
public static void main(String[] args) {
ArrayCheckEquality ap=new ArrayCheckEquality();
boolean equal;
try {
int[] intArray1 = {1,2,3,4};
int[] intArray2 = {1,2,3,4};
printArraySummary(intArray1, "Original Array1");
printArraySummary(intArray2, "Original Array2");
equal=ap.arrayCheckEquality(intArray1, intArray2);
if (equal==true) {
System.out.println("The arrays are equal !");
}else {
System.out.println("The arrays are *not* equal !");
}
int[] intArray3 = {7,8};
int[] intArray4 = {9,8};
printArraySummary(intArray3, "Original Array1");
printArraySummary(intArray4, "Original Array2");
equal=ap.arrayCheckEquality(intArray3, intArray4);
if (equal==true) {
System.out.println("The arrays are equal !");
}else {
System.out.println("The arrays are *not* equal !");
}
int[] intArray5 = {10,20};
int[] intArray6 = {30,40,50};
printArraySummary(intArray5, "Original Array1");
printArraySummary(intArray6, "Original Array2");
equal=ap.arrayCheckEquality(intArray5, intArray6);
if (equal==true) {
System.out.println("The arrays are equal !");
}else {
System.out.println("The arrays are *not* equal !");
}
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}