I'm trying to determine whether or not two 2d arrays are equivalent in terms of integer values. When I input the following values for the arrays:
First Array:
1 1 1
1 2 2
1 1 1
Second Array:
1 1 1
1 1 1
1 1 1
//I get:
Equivalent
//which is not true, since the arrays are not identical.
I'm not sure what I'm doing wrong. My code for this problem is attached below. Any help would be greatly appreciated.
P.S. This is my first time posting on S.O., so please forgive me and correct me if I've done anything wrong in terms of formatting anything. Thank you!
import java.util.*;
public class TwoDimensionalArrayEquivalence {
public static void main(String[] args) {
//initialize the first two arrays
int[][] firstArray = new int[3][3];
int[][] secondArray = new int[3][3];
Scanner scan = new Scanner(System.in);
//ask user to input first array
System.out.println("Please input an array of 9 numbers: ");
int userInput = 0;
for (int row = 0; row < firstArray.length; row++) {
for (int column = 0; column < firstArray.length; column++) {
userInput = scan.nextInt();
firstArray[row][column] = userInput;
}
}
//ask the user to input the second array
System.out.println("\nPlease input another array of 9 numbers: ");
int userInput2 = 0;
for (int row = 0; row < secondArray.length; row++) {
for (int column = 0; column < secondArray.length; column++) {
userInput2 = scan.nextInt();
secondArray[row][column] = userInput2;
}
}
//print the first array user has input
System.out.println("\nFirst Array:");
for (int row = 0; row < firstArray.length; row++) {
for (int column = 0; column < firstArray[row].length; column++) {
System.out.print(firstArray[row][column] + " ");
}
System.out.println();
}
//print the second array user has input
System.out.println("\nSecond Array:");
for (int row = 0; row < secondArray.length; row++) {
for (int column = 0; column < secondArray[row].length; column++) {
System.out.print(secondArray[row][column] + " ");
}
System.out.println();
}
//call method CheckArrayEquality to check for array equivalence
CheckArrayEquality(firstArray, secondArray);
}
public static void CheckArrayEquality(int[][] firstArray, int[][] secondArray){
boolean decider = false;
//here, I'm trying to traverse the arrays by incrementing by each row and column
for (int row = 0; row < firstArray.length; row++) {
for (int column = 0; column < firstArray[row].length; column++) {
//Below: if the value in a specific row and column in firstArray is equal to
//the value in the same specific row and column in secondArray, then decider is
//"true". Else, decider is "false"
if(firstArray[row][column] == secondArray[row][column]){
decider = true;
}
else {
decider = false;
}
}
}
//if-else statement for printing out whether the arrays are equivalent or not
//based on what the decider has become
if (decider == false) {
System.out.println("\nNot equivalent");
}
else if (decider == true) {
System.out.println("\nEquivalent");
}
}
}
You overwrite the value of decider
in every iteration, which means the only value you'll get in the end is if the last entries are equal.
A better implementation would be:
public static boolean array2dEquals(int[][] firstArray, int[][] secondArray){
if(firstArray.length != secondArray.length) return false;
for (int row = 0; row < firstArray.length; row++) {
if(firstArray[row].length != secondArray[row].length) return false;
for (int column = 0; column < firstArray[row].length; column++) {
if(firstArray[row][column] != secondArray[row][column]) return false;
}
}
return true;
}
usage:
if(array2dEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}
This implementation also checks the array length first, so you won't get any ArrayIndexOutOfBoundsException
if the arrays do not match in size.
But unless you care about every last bit of performance, it would be best to just use what java already provides:
if(Arrays.deepEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}