Java 打印数组的递归方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19306336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive method to print array
提问by MQSJ23
This is the method code:
这是方法代码:
public static void printMatrix(int[][] m, int i, int j) {
if (i == m.length ||j==m.length) {
System.out.println();
} else {
System.out.print("[" + m[i][j] + "]");
printMatrix(m, i, j++);
printMatrix(m, i++, j);
}
}
I don′t know why it just prints the first position of the array until a StackOverFlow error.
我不知道为什么它只打印数组的第一个位置,直到出现 StackOverFlow 错误。
Thanks for the help.
谢谢您的帮助。
采纳答案by lordkain
You call 2 times the recursive function, but it keep calling itself with i and j..
你调用了 2 次递归函数,但它一直用 i 和 j 调用自己。
printMatrix(m, i, j++); << use ++j
printMatrix(m, i++, j); << use ++i
Here is a possible solution for you
这是一个可能的解决方案
public static void printMatrix(int[][] m, int i, int j)
{
System.out.print("[" + m[i][j] + "]");
if (i == m.length && j == m.length)
{
return;
}
if (j == m.length)
{
j = 0;
++i;
printMatrix(m, i, j);
}
else
{
j++;
printMatrix(m, i, j);
}
}
non-recursive
非递归
public static void printMatrix(int[][] m)
{
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m.length; j++)
System.out.print("[" + m[i][j] + "]");
}
回答by ajb
If you're trying to print each element of the matrix once, then none of the solutions in the other answers [EDIT: I guess we're down to one answer now] is going to help. The most they will do is get rid of the stack overflow error, but the output is still not going to be close to what you need.
如果您尝试将矩阵的每个元素打印一次,那么其他答案中的任何解决方案 [编辑:我想我们现在只剩下一个答案] 都会有所帮助。他们最多会做的是摆脱堆栈溢出错误,但输出仍然不会接近您需要的。
Assuming this is a homework assignment and you've been told to use recursion for some reason (in real life nobody would do that), you have to step back and think about this: just what do you want printMatrix(m,i,j)
to do? Presumably, you want to print the m[i][j]
element, and then call printMatrix
to print the rest of the matrix. When you call printMatrix
recursively to start the printing rest of the matrix, what do you want i
and j
to be? Probably, you want the same i
and the next column, j+1
, but not if j
is at the end of the row. Then you want ... I'll let you think about that. But I don't think you want printMatrix
to call itself twice. Instead, you want it to call itself only once (at most); you'll probably need an if
statement that looks something like
假设这是一项家庭作业,并且您被告知出于某种原因使用递归(在现实生活中没有人会这样做),您必须退后一步思考:您想做printMatrix(m,i,j)
什么?据推测,您想打印m[i][j]
元素,然后调用printMatrix
以打印矩阵的其余部分。当您printMatrix
递归调用以开始打印矩阵的其余部分时,您想要i
和j
成为什么?可能,您想要相同的i
和下一列, j+1
,但如果j
在行的末尾则不需要。那么你想要......我会让你考虑一下。但我不认为你想printMatrix
给自己打电话两次。相反,您希望它只调用一次(最多);你可能需要一个if
看起来像的语句
if(something)
printMatrix(something);
else
printMatrix(something different);
but it will still call itself only once (since it will pick one or the other).
但它仍然只会调用自己一次(因为它会选择一个或另一个)。
I'll mention one other thing: you're comparing i
to the number of rows in the array (m.length
), but you're also comparing j
to the number of rows in the array. That's fine if you know this is a square matrix. But if you want to compare j
to the number of columns, compare it to m[i].length
, since m[i]
is itself an array (that represents one row of the matrix).
我还要提一件事:您正在与i
数组 ( m.length
) 中j
的行数进行比较,但您也在与数组中的行数进行比较。如果你知道这是一个方阵就好了。但是,如果您想与j
列数进行比较,请将其与 进行比较m[i].length
,因为m[i]
它本身就是一个数组(表示矩阵的一行)。
回答by Sandhiya Ram
The size of array 'm' will be constant through out the recursive calls. Whereas the value of of i and j will be changing and base condition will be only satisfied once that. So it infinitely enter the base condition was the same i and j. That is why i guess it keeps printing only one value and after sometime the stack overflows. i do not really view this as proper use of recursion. Correct me if i'm wrong. when using recursion a problem reduces size as function calls are made till it is broken into the smallest unit possible, which can be identified by the base condition or the breaking point. I don't see this happening here.
数组“m”的大小在整个递归调用中将保持不变。而 i 和 j 的值会发生变化,并且基本条件只会满足一次。所以它无限进入基本条件是相同的i和j。这就是为什么我猜它只打印一个值,一段时间后堆栈溢出。我并不认为这是正确使用递归。如果我错了纠正我。使用递归时,问题会随着函数调用的进行而减小,直到它被分解成可能的最小单元,这可以通过基本条件或断点来识别。我没有看到这里发生这种情况。
public class Printarray {
static int max= 2;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int array[]={1,2,3};
print(array,0);
}
public static void print(int array[],int i)
{
System.out.println(array[i]);
if(i==Printarray.max)
{
Printarray.max--;
return;
}
else
{
print(array,i+1);
}
}
}
This works for 1D array, you can try this for 2D arrays and see if it works. I hope it helps!
这适用于一维数组,你可以试试这个用于二维数组,看看它是否有效。我希望它有帮助!
回答by Rahul
private static void print(int[][] mat, int i, int j) {
// TODO Auto-generated method stub
if(mat==null){
return;
}
if(i==mat.length || j==mat[0].length){
return;
}
System.out.print(mat[i][j]+" ");
if(j==mat[0].length-1){
System.out.println();
print(mat,i+1,0);
}
print(mat,i,j+1);
}