Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我正在写一个比较两个数组的程序,但是我可以';无法将数组与空元素进行比较_Java_Arrays - Fatal编程技术网

Java I';我正在写一个比较两个数组的程序,但是我可以';无法将数组与空元素进行比较

Java I';我正在写一个比较两个数组的程序,但是我可以';无法将数组与空元素进行比较,java,arrays,Java,Arrays,我的代码粘贴在下面。该程序在不同类型的二维数组上运行良好,如果它们相同,则返回“true”,如果它们不相同,则返回false。但是,当两个数组的两个维度都有空元素时,会出现一个小错误: int a[][] = {{},{}}; int b[][] = {{},{}}; 这个输入需要返回“true”,因为数组仍然相同,但是我得到了一个数组索引越界错误。有没有办法让我的程序识别出这两个数组仍然相同 public class ArrayCompare { public sta

我的代码粘贴在下面。该程序在不同类型的二维数组上运行良好,如果它们相同,则返回“true”,如果它们不相同,则返回false。但是,当两个数组的两个维度都有空元素时,会出现一个小错误:

    int a[][] = {{},{}};
    int b[][] = {{},{}};
这个输入需要返回“true”,因为数组仍然相同,但是我得到了一个数组索引越界错误。有没有办法让我的程序识别出这两个数组仍然相同

public class ArrayCompare {
    public static void main(String[] args){
        int a[][] = {{},{}};
        int b[][] = {{},{}};
        boolean result = equals(a,b);
        System.out.println(result);
    }

    public static boolean equals(int[][] a, int[][] b) {
        boolean boo = true;
        if (a != null && b != null) {
          if (a.length != b.length || a[0].length != b[0].length || a[1].length != b[1].length)
              boo = false;
          else
              for (int i = 0; i < b.length; i++) {
                  for(int j =0; j <b.length; j++) {
                      if (b[i][j] != a[i][j]) {
                          boo = false;    
                      }   
                  }

            }
        }else {
          boo = false;
        }
        return boo;
    }
}
公共类ArrayCompare{
公共静态void main(字符串[]args){
int a[][]={{},{};
int b[][]={{},{};
布尔结果=等于(a,b);
系统输出打印项次(结果);
}
公共静态布尔等于(int[]a,int[]b){
布尔boo=true;
如果(a!=null&&b!=null){
如果(a.length!=b.length | | a[0]。length!=b[0]。length | | a[1]。length!=b[1]。length)
boo=false;
其他的
for(int i=0;i对于(int j=0;j,代码中存在一个小的逻辑缺陷。在嵌套循环中,您应该迭代每个数组中的元素

for (int i = 0; i < b.length; i++) {
    for(int j =0; j <b[i].length; j++) {// iterate over the number of elements of the current array
        if (b[i][j] != a[i][j]) {
            boo = false;
        }
    }
}
for(int i=0;i对于(int j=0;j,代码中存在一个小的逻辑缺陷。在嵌套循环中,您应该迭代每个数组中的元素

for (int i = 0; i < b.length; i++) {
    for(int j =0; j <b[i].length; j++) {// iterate over the number of elements of the current array
        if (b[i][j] != a[i][j]) {
            boo = false;
        }
    }
}
for(int i=0;i对于(int j=0;j将此检查添加到
else
语句
a[i]。长度>0

else {
    for (int i = 0; i < b.length; i++) {
        if (a[i].length > 0) {  // add check for empty array
            for (int j = 0; j < b.length; j++) {
                //...
            }
        }
    }
}
else{
for(int i=0;i0){//添加空数组检查
对于(int j=0;j
p.S.

您的代码经过一些修改后运行良好。我可能会给您一些想法,如何改进它。这一个呢:

public static boolean equals(int[][] one, int[][] two) {
    if (one == null || two == null || one.length != two.length)
        return false;

    for (int row = 0; row < one.length; row++) {
        if (one[row].length != two[row].length)
            return false;

        for (int col = 0; col < one[row].length; col++)
            if (one[row][col] != two[row][col])
                return false;
    }

    return true;
}
公共静态布尔等于(int[][]one,int[]two){
if(one==null | | two==null | | one.length!=two.length)
返回false;
for(int row=0;row<1.length;row++){
if(一[row].length!=两[row].length)
返回false;
for(int col=0;col<1[行]。长度;col++)
如果(一[行][col]!=两[行][col])
返回false;
}
返回true;
}

将此检查添加到
else
语句
a[i]。长度>0

else {
    for (int i = 0; i < b.length; i++) {
        if (a[i].length > 0) {  // add check for empty array
            for (int j = 0; j < b.length; j++) {
                //...
            }
        }
    }
}
else{
for(int i=0;i0){//添加空数组检查
对于(int j=0;j
p.S.

您的代码经过一些修改后运行良好。我可能会给您一些想法,如何改进它。这一个呢:

public static boolean equals(int[][] one, int[][] two) {
    if (one == null || two == null || one.length != two.length)
        return false;

    for (int row = 0; row < one.length; row++) {
        if (one[row].length != two[row].length)
            return false;

        for (int col = 0; col < one[row].length; col++)
            if (one[row][col] != two[row][col])
                return false;
    }

    return true;
}
公共静态布尔等于(int[][]one,int[]two){
if(one==null | | two==null | | one.length!=two.length)
返回false;
for(int row=0;row<1.length;row++){
if(一[row].length!=两[row].length)
返回false;
for(int col=0;col<1[行]。长度;col++)
如果(一[行][col]!=两[行][col])
返回false;
}
返回true;
}

您正在检查
a[0]。length
!=
b[0]。在检查数组是否为空之前,length
。使用连接到字符串不是更快吗?那么,比较连接字符串?您正在检查
a[0]。length
!=
b[0].length
检查数组是否为空之前。使用连接到字符串不是更快吗?因此,比较连接字符串?