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 递归打印2d数组时出现问题_Java_Arrays - Fatal编程技术网

Java 递归打印2d数组时出现问题

Java 递归打印2d数组时出现问题,java,arrays,Java,Arrays,递归打印二维数组时,该方法打印两列,并用三个额外间隔重复第二列两次。我该如何解决这个问题 完整代码: public static void main(String [] args) { final int ROWS = 2, COLS = 2; long [][] arr = new long [ROWS][COLS]; setArray(arr, 0, 0); System.out.println("Numbers in 2d Array: "); p

递归打印二维数组时,该方法打印两列,并用三个额外间隔重复第二列两次。我该如何解决这个问题

完整代码:

public static void main(String [] args) {
    final int ROWS = 2, COLS = 2;
    long [][] arr = new long [ROWS][COLS];
    setArray(arr, 0, 0);

    System.out.println("Numbers in 2d Array: ");
    printArray(arr, 0, 0);

}

public static void setArray(long [][] a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            a[r][c] = (long)(Math.random() * 100);
            setArray(a, r, ++c);
        }
        setArray(a, ++r, c = 0);
    }
}

public static void printArray(long [][]a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
        }
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}
publicstaticvoidmain(字符串[]args){
最终int行=2,COLS=2;
long[]arr=新的long[行][COLS];
setArray(arr,0,0);
System.out.println(“二维数组中的数字:”);
打印阵列(arr,0,0);
}
公共静态void setArray(长[][]a,int r,int c){
if(r
我遇到问题的方法:

public static void printArray(long [][] a, int r, int c){
    if(r < a.lenght){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
        }
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}
publicstaticvoidprintary(长[][]a,int r,int c){
if(r
预期产出:

二维数组中的数字:

74 16

4491

实际产量:

二维数组中的数字:

74 16

4491

4491


44 91

看起来像是一个家庭作业问题。因此,我建议通过执行来发现问题。如果你仍然被困住,我的提示是考虑什么时候回来

问题在于,当您在第二个
if
语句中打印单个单元格值时,您没有退出
printary()
方法。这意味着,当您打印单个单元格值时,您将始终执行
System.out.println()也在末尾。这将导致大量新行和数组中没有意义的部分的输出

添加行
System.out.println(“用r=“+r+”,c=“+c调用”)时
printary()
方法的开头,您将获得以下输出,用于调试目的:

Numbers in 2d Array: 
Called with r=0,c=0
89 Called with r=0,c=1
59 Called with r=0,c=2

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0
正如您在调试行中看到的,行和列索引不会一直上升,而是从行索引
2
返回到索引
1
。这应该是您的
printArray()有什么问题的指示器方法

要解决此问题,只需使用
返回if()
中使用code>语句提前退出方法(不要调用
System.out.println();
行),或者使用
if-else
语句打印单元格值或生成新行

public static void printArray(long [][]a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
            return; //  <----------- here
        } // <--- or use an "else" block here
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}
publicstaticvoidprintary(长[][]a,int r,int c){
if(rreturn;//什么是输入(数组),什么是实际输出,以及您期望它是什么?Show a,即显示数组是如何创建的,以及如何调用
printary
。哇,我不敢相信这只是一个需要放置的简单else语句。非常感谢!