我有一个矩阵形式的数组
int[][] matrix=new int[3][3];
//comparing the numbers of row 1
if(matrix[0][0]==1 && matrix[0][1]==0 && matrix[0][2]==1){
System.out.println("Some Message");
}
这很好用,但是有没有比编写更小、更快的方法
if(matrix[0][0]==1 && matrix[0][1]==0 && matrix[0][2]==1)
比较值,因为我必须对矩阵的第 2 行和第 3 行执行相同的操作
最佳答案
您可以为此使用循环。
int[][] matrix=new int[3][3];
for(int i = 0 ; i < 3 ; i++) {
boolean result = true;
for(int j = 0 ; j < 3 ; j++) {
if(matrix[i][j] != (j+1)%2) {
result = false;
break;
}
}
if(result) {
System.out.println("Some Message");
}
}
关于java - 有没有更好/更小的方法来比较矩阵的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57752255/