java - 二维阵列等效?
问题描述
我试图确定两个二维数组在整数值方面是否相等。当我为数组输入以下值时:
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.
我不确定我做错了什么。我的这个问题的代码附在下面。任何帮助将不胜感激。
PS这是我第一次在SO上发帖,所以如果我在格式化任何内容方面做错了什么,请原谅我并纠正我。谢谢!
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");
}
}
}
解决方案
您在每次迭代中覆盖 的值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;
}
用法:
if(array2dEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}
ArrayIndexOutOfBoundsException
此实现还首先检查数组长度,因此如果数组大小不匹配,您将不会得到任何结果。
但是除非你关心每一点性能,否则最好只使用 java 已经提供的:
if(Arrays.deepEquals(firstArray, secondArray)) {
System.out.println("\nEquivalent");
} else {
System.out.println("\nNot equivalent");
}
推荐阅读
- blockchain - dapp 是否需要元掩码?
- python - 抓取的数据不会保存到 csv 文件,因为它一直返回一个空白的 csv 文件。
- python - 有没有更有效的实施方式?
- angular - 从 QueryList 中识别特定的 Angular TemplateRef
- vue.js - 使用 Sails JS 和 Vue JS 上传文件
- android - 如何使用 XML 文档创建这样的 XML 文件?
- flutter - 颤振在运行时创建小部件
- node.js - Node.js 中的 Python `getattr` 等价物是什么?
- azure - Azure Kubernetes 上的自定义域和大使 API 网关
- android - 为什么永远不会调用 onLocationchanged