w3resource

Java generic method: Check equality of arrays

Java Generic: Exercise-1 with Solution

Write a Java program to create a generic method that takes two arrays of the same type and checks if they have the same elements in the same order.

Sample Solution:

Java Code:

// ArrayCompare.java
// ArrayCompare Class
import java.util.Arrays;
public class ArrayCompare {
  public static < T > boolean compare_Arrays(T[] array1, T[] array2) {
    if (array1.length != array2.length) {
      return false;
    }

    for (int i = 0; i < array1.length; i++) {
      if (!array1[i].equals(array2[i])) {
        return false;
      }
    }
    return true;
  }

  public static void main(String[] args) {
    Integer[] arr1 = {
      1,
      2,
      3,
      4
    };
    Integer[] arr2 = {
      1,
      2,
      4,
      4
    };
    Integer[] arr3 = {
      1,
      2,
      3,
      4
    };
    String[] arr4 = {
      "Java",
      "World"
    };
    String[] arr5 = {
      "JavaScript",
      "World"
    };
    String[] arr6 = {
      "Java",
      "World"
    };
    System.out.println("Original arrays:");
    System.out.println("arr1: " + Arrays.toString(arr1));
    System.out.println("arr2: " + Arrays.toString(arr2));
    System.out.println("arr3: " + Arrays.toString(arr3));
    System.out.println("arr4: " + Arrays.toString(arr4));
    System.out.println("arr5: " + Arrays.toString(arr5));
    System.out.println("arr6: " + Arrays.toString(arr6));
    System.out.println("\nCompare arr1 and arr2: " + compare_Arrays(arr1, arr2)); //false
    System.out.println("Compare arr1 and arr3: " + compare_Arrays(arr1, arr3)); //true
    System.out.println("Compare arr4 and arr5: " + compare_Arrays(arr4, arr5)); //false
    System.out.println("Compare arr4 and arr6: " + compare_Arrays(arr4, arr6)); //true     		
  }
}

Sample Output:

 Original arrays:
arr1: [1, 2, 3, 4]
arr2: [1, 2, 4, 4]
arr3: [1, 2, 3, 4]
arr4: [Java, World]
arr5: [JavaScript, World]
arr6: [Java, World]

Compare arr1 and arr2: false
Compare arr1 and arr3: true
Compare arr4 and arr5: false
Compare arr4 and arr6: true

Explanation:

In the above exercise, we define a generic method compareArrays() that takes two arrays of the same type T. The method compares the elements of both arrays using the equals() method. If any pair of elements at the same index is not equal, the method returns false. If all elements are equal and the array lengths are the same, the method returns true.

The main() method tests the compareArrays() method with integers and strings to demonstrate its functionality.

Flowchart:

Flowchart: Java generic method: Check equality of arrays.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Java Generic Method Exercises Home.
Next: Calculate sum of even and odd numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/generic/java-generic-exercise-1.php