java.lang.StackOverFlowError表示5位或更多的值

时间:2014-09-24 01:33:46

标签: java stack-overflow

以下是我的代码应该如何工作。首先,它在变量t中需要许多测试用例。现在文本案例的数量可以达到100000.然后它需要两个输入,一个用于行,另一个用于列。输入样本 -

3
2 3
6 7
10 10

应该发生的是这个 -

We have the following matrix 
1 0 0 0 0 0 ...
2 2 0 0 0 0 ...
3 3 3 0 0 0 ...
4 4 4 4 0 0 ... 
5 5 5 5 5 0 ... 
6 6 6 6 6 6 ...
and so on ... 
The matrix is created as follows, first row contains one 1 and rest 0's, second row
contains 2 twos and rest zeros, third row contains 3 threes and so on.

Given R and C, calculate the count of even and odd numbers in sub matrix[R,C].
0 is neither odd nor even and 1 based indexing is used i.e. matrix[1,1]=1

Output
For each test case print count of even and odd numbers in sub matrix[R,C].

输出样本 -

2 1
12 9
30 25

现在一切都适用于我的代码中的小数字。行和列的最大值应该是每个100000。问题发生的地方。对于行或列中的输入值25000,代码工作正常,但如果我为其中任何一个输入大约30000或更高的值,我会得到stackoverflower错误。

这是我的完整代码 -

import java.util.Scanner;

public class TestClass {
public static void main(String args[]) throws Exception {

    Scanner input = new Scanner(System.in);
    int t = input.nextInt();
    int[][] storage = new int[t][2];

    for (int i = 0; i < t; i++) {
        for (int j = 0; j < 2; j++) {
            int x = input.nextInt();
            storage[i][j] = x;

        }
    }
    TestClass test = new TestClass();
    for (int i = 0; i < storage.length; i++) {
        int a = storage[i][0];
        int b = storage[i][1];
        test.theMethod(a, b);
    }
    input.close();
}

public void theMethod(int x, int y) {
    int d = x - y;
    int first = 0;
    int second = 0;
    int[][] resultarray = new int[1][2];
    if (x % 2 == 0) {
        if (x >= 1) {
            if (y >= x) {
                first = number(x);
                second = number(x - 1) + 1;
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            } else if (y < x) {
                first = (number(x) - number(d)) - 1;
                second = (number(x - 1) - number(d - 1)) + 1;
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            }
        }
    } else if (x % 2 != 0) {
        if (x >= 1) {
            if (y >= x) {
                first = number(x) + 1;
                second = number(x - 1);
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            } else if (y < x) {
                first = (number(x) - number(d));
                second = number(x - 1) - number(d - 1);
                resultarray[0][0] = second;
                resultarray[0][1] = first;
            }
        }
    }
    for (int i = 0; i < resultarray.length; i++) {
        for (int j = 0; j < resultarray[i].length; j++) {
            System.out.print(resultarray[i][j]);
            System.out.print(" ");
        }
        System.out.println();
    }

}

public int number(int x) {
    if (x == 1 || x == 0) {
        x = 0;
    } else if (x > 1) {
        x = x + number(x - 2);      //*****this is the error line
    }
    return x;
}
}

错误堆栈跟踪 -

Exception in thread "main" java.lang.StackOverflowError
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)

现在,对于大于25000或大约30000的值,可能会显示此错误。有一件事我想提一下,我已经尝试过对这个程序中的每个int值使用long,但是我仍然得到相同的错误,甚至更低的输入值。谁知道我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要停止使用递归。由于number方法中的递归调用,您的堆栈空间不足。您可以修改方法以使用循环而不是递归调用自身。这样可以防止你的堆栈空间不足。

答案 1 :(得分:0)

堆栈跟踪非常清楚地说明导致堆栈溢出的原因,即number()方法。

只需简单了解一下,对于每个号码nn/2的递归调用大约为number()级。如果你传递30000,那么在调用堆栈中这将是15000个级别,这是相当多的。

您的number()实际上可以使用这样的简单循环重写:

int number(int x) {
    int result = 0;

    while (x > 1) {
       result += x;
       x -= 2;
    }
    return result;
}