Java: Test the equality of two arrays
Write a Java program to test two arrays' equality.
Pictorial Presentation:
Sample Solution:
Java Code :
// Define a class named Exercise23.
public class Exercise23 {
// Create a static method named equality_checking_two_arrays that takes two integer arrays.
static void equality_checking_two_arrays(int[] my_array1, int[] my_array2) {
// Initialize a boolean variable to indicate whether the arrays are equal.
boolean equalOrNot = true;
// Check if the lengths of the two arrays are equal.
if (my_array1.length == my_array2.length) {
// Iterate through elements of both arrays.
for (int i = 0; i < my_array1.length; i++) {
// Check if the elements at the same position in both arrays are not equal.
if (my_array1[i] != my_array2[i]) {
equalOrNot = false;
}
}
} else {
// If the lengths are not equal, set equalOrNot to false.
equalOrNot = false;
}
// Check the value of equalOrNot and print whether the arrays are equal or not.
if (equalOrNot) {
System.out.println("Two arrays are equal.");
} else {
System.out.println("Two arrays are not equal.");
}
}
// The main method for executing the program.
public static void main(String[] args) {
// Define three integer arrays for comparison.
int[] array1 = {2, 5, 7, 9, 11};
int[] array2 = {2, 5, 7, 8, 11};
int[] array3 = {2, 5, 7, 9, 11};
// Call the equality_checking_two_arrays method to compare array1 and array2.
equality_checking_two_arrays(array1, array2);
// Call the equality_checking_two_arrays method to compare array1 and array3.
equality_checking_two_arrays(array1, array3);
}
}
Sample Output:
Two arrays are not equal. Two arrays are equal.
Flowchart:
Java Code Editor:
Previous: Write a Java program to find all pairs of elements in an array whose sum is equal to a specified number.
Next: Write a Java program to find a missing number in an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics