Java 二维阵列等效?
我试图确定两个2d数组在整数值方面是否相等。当我为数组输入以下值时:Java 二维阵列等效?,java,arrays,multidimensional-array,equivalence,Java,Arrays,Multidimensional Array,Equivalence,我试图确定两个2d数组在整数值方面是否相等。当我为数组输入以下值时: 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. 我不确定我做错了什么。这个问题的代码附在下面。任何帮助都将不胜感激 另外,这是我第一次在S.O.上发帖,所以如果我在格式方面有任何错
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.
我不确定我做错了什么。这个问题的代码附在下面。任何帮助都将不胜感激
另外,这是我第一次在S.O.上发帖,所以如果我在格式方面有任何错误,请原谅我并纠正我。谢谢大家!
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");
}
}
}
import java.util.*;
公共类二维数组等价性{
公共静态void main(字符串[]args){
//初始化前两个数组
int[]firstArray=新的int[3][3];
int[]secondArray=newint[3][3];
扫描仪扫描=新扫描仪(System.in);
//要求用户输入第一个数组
System.out.println(“请输入9个数字的数组:”);
int userInput=0;
for(int row=0;row
实际上,如果矩阵的最后一个整数等于第二个整数,则应用程序所做的就是返回等效值
例子
修正:
publicstaticvoidcheckarrayequality(int[]firstArray,int[]secondArray){
布尔决策器=假;
//在这里,我试图通过按每行和每列递增来遍历数组
linesLoop:
for(int row=0;row
我想你是在参照比较。数组是对象,因此必须使用equals
。或者更好,从阵列库:
类测试{
公共静态void main(字符串[]args){
int[]arr={{1,2},{3,4};
int[]arr2={{1,2},{3,4};
System.out.println(checkequals(arr,arr2));
}
私有静态布尔checkequals(int[]arr,int[]arr2){
返回数组.deepEquals(arr,arr2);
}
}
在每次迭代中覆盖decider
的值,这意味着最后得到的唯一值是最后的条目是否相等
更好的实施方式是:
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;
}
此实现还首先检查数组长度,因此如果数组大小不匹配,则不会获得任何ArrayIndexOutOfBoundsException
但除非您关心每一点性能,否则最好只使用java已经提供的功能:
if(Arrays.deepEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}
您正在打印决策器的最后一个值,因此它是值
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;
}
if(array2dEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}
if(Arrays.deepEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}