Reputation: 21
I need to check if the rows of one matrix are equal to rows of a second matrix. I've tried a few different methods but I can't seem to find a simple method that works.
Here is the file I am using for testing:
public static void main(String[] args) {
int[][] a0 = null;
int[][] b0 = null;
int[][] a1 = {};
int[][] b1 = {};
System.out.println(ex1(a0,b0)==false);
System.out.println(ex1(a0,b1)==false);
System.out.println(ex1(a1,b0)==false);
System.out.println(ex1(a1,b1)==false);
int[][] a2 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b2 = {{4, 7 , 1 }
,{3, 5 , 10 }
,{2, 6 , 100}};
System.out.println(ex1(a2,b2)==true );
int[][] a3 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b3 = {{4, 1 , 7}
,{3, 10 , 6}
,{2, 0 , 5}};
System.out.println(ex1(a3,b3)==false);
int[][] a4 = {{2, 1 , 5}
,{3, 10 , 6}
,{4, 100, 7}};
int[][] b4 = {{1 , 4 , 7}
,{10, 3 , 6}
,{0 , 2 , 5}};
System.out.println(ex1(a4,b4)==false);
int[][] a5 = {{1 , 2, 5 }
,{10 , 3, 6 }
,{100, 4, 7 }};
int[][] b5 = {{1 , 4, 1 }
,{10 , 3, 10 }
,{0 , 2, 100}};
System.out.println(ex1(a5,b5)==true );
int[][] a6 = {{1 , 2 , 5}
,{10 , 3 , 6}
,{100, 4 , 7}};
int[][] b6 = {{1 , 1 , 7}
,{1 , 10 , 6}
,{0 , 100, 5}};
System.out.println(ex1(a6,b6)==true );
}
}
and this is my code:
public static boolean ex1(int[][] a, int[][] b){
if(a == null || a.length < 1 || b == null || b.length < 1){
return false;
}
for(int i = 0; i < a.length; i++){
int cont = 0;
for(int j = 0; j < a.length; j++){
for(int z = 0; z < a.length; z++){
if(a[j][i] == b[z][i]){
cont++;
}
if(cont == a[i].length){
return true;
}
}
}
}
return false;
}
Here is the output of the testing file, all should be true: Output Image
Can someone please help me?
Upvotes: 0
Views: 85
Reputation: 9041
I would change up your ex1()
so that you check for conditions where they don't match and return false. If you get to the bottom of the method, then the matrices are equal.
Something like:
public class StackOverflow {
public static void main(String[] args) {
int[][] a0 = null;
int[][] b0 = null;
int[][] a1 = {};
int[][] b1 = {};
System.out.println(ex1(a0, b0) == false);
System.out.println(ex1(a0, b1) == false);
System.out.println(ex1(a1, b0) == false);
System.out.println(ex1(a1, b1) == false);
int[][] a2 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
int[][] b2 = { { 4, 7, 1 }, { 3, 5, 10 }, { 2, 6, 100 } };
System.out.println(ex1(a2, b2) == true);
int[][] a3 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
int[][] b3 = { { 4, 1, 7 }, { 3, 10, 6 }, { 2, 0, 5 } };
System.out.println(ex1(a3, b3) == false);
int[][] a4 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
int[][] b4 = { { 1, 4, 7 }, { 10, 3, 6 }, { 0, 2, 5 } };
System.out.println(ex1(a4, b4) == false);
int[][] a5 = { { 1, 2, 5 }, { 10, 3, 6 }, { 100, 4, 7 } };
int[][] b5 = { { 1, 4, 1 }, { 10, 3, 10 }, { 0, 2, 100 } };
System.out.println(ex1(a5, b5) == true);
int[][] a6 = { { 1, 2, 5 }, { 10, 3, 6 }, { 100, 4, 7 } };
int[][] b6 = { { 1, 1, 7 }, { 1, 10, 6 }, { 0, 100, 5 } };
System.out.println(ex1(a6, b6) == true);
}
public static boolean ex1(int[][] a, int[][] b) {
if (a == null || a.length < 1 || b == null || b.length < 1) {
return false;
}
// Check that both arrays have the same number of rows
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
// Check that each row has the same number of columns
if (a[i].length != b[i].length) {
return false;
}
// Check the contents of each row
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] != b[i][j]) {
return false;
}
}
}
return true;
}
}
Result:
true
true
true
true
false // These don't match, but you're checking that they do match
true // These don't match, and you're checking that they don't match
true // These don't match, and you're checking that they don't match
false // These don't match, but you're checking that they do match
false // These don't match, but you're checking that they do match
With the data you are showing in your question, all of them will not evaluate to true.
Upvotes: 1
Reputation: 1000
You can simply use the function Arrays.equals(arr1, arr1). Although the example is for 1d array, should work for 2d arrays:
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
int[] ary = {1,2,3,4,5,6};
int[] ary1 = {1,2,3,4,5,6};
int[] ary2 = {1,2,3,4};
System.out.println("Is array 1 equal to array 2?? " +Arrays.equals(ary, ary1));
System.out.println("Is array 1 equal to array 3?? " +Arrays.equals(ary, ary2));
}
}
The output:
Is array 1 equal to array 2?? true
Is array 1 equal to array 3?? false
Upvotes: 3