gpt4 book ai didi

java - 二维数组等价?

转载 作者:行者123 更新时间:2023-12-02 09:15:28 25 4
gpt4 key购买 nike

我试图确定两个二维数组在整数值方面是否相等。当我为数组输入以下值时:

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");
}
}
}

最佳答案

您会在每次迭代中覆盖 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");
}

关于java - 二维数组等价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com