在StackOverflowError之前执行了多少次迭代

时间:2015-08-18 08:09:07

标签: java android recursion stack-overflow

我目前正在编写一个Android检查编写程序,它可以获取用户输入(阿拉伯数字)并将其转换为检查式数字。例如,四千五百美元和无分数,类似的东西。英语部分很简单,我完成了它。现在我在一个不同的活动中写一个中文版。这次我使用了递归算法,因为中文数字的编写方式截然不同。这是算法:(评论是翻译)

private String convertNumberString(String number) {
    if (number.equals ("")) {
        return "請輸入幣值"; //Please enter amount
    }

    String[] twoParts = number.split ("\\.");
    String integerPart = twoParts[0];
    String decimalPart;
    try {
        decimalPart = twoParts[1];
    } catch (ArrayIndexOutOfBoundsException ex) {
        decimalPart = "";
    }

    if (new BigInteger (integerPart).compareTo (new BigInteger ("9999999999999999")) > 0) {
        return "輸入的幣值過大,必須小於或等於 9999999999999999"; //The amount is too large, must be less than or equal to 9999999999999999
    }

    if (new BigInteger (integerPart).compareTo (new BigInteger ("1")) < 0) {
        return "輸入的幣值過小,必須大於或等於 1"; //The amount is too small, must be greater than or equal to 1
    }

    String integerString;
    String decimalString = "";

    integerString = convertInteger (integerPart);

    if (decimalPart.equals ("") || Integer.parseInt (decimalPart) == 0) {
        decimalString = "";
    } else {
        if (decimalPart.length () < 2) {
            decimalPart += "0";
        }
        String jiao = decimalPart.substring (0, 1); //jiao = 角
        String fen = decimalPart.substring (1, 2); // fen = 分
        if (!jiao.equals ("0")) {
            decimalString += convertNumber (getDigitFromString (jiao, 0)) + "角"; // 角 = 10 cents 分 = 1 cent
        }

        if (!fen.equals ("0")) {
            decimalString += convertNumber (getDigitFromString (fen, 0)) + "分";
        }
    }

    return integerString + "圓" + decimalString + "正"; //圓 = dollars 正 = only
}

private String convertNumber (int i) {
    switch (i) {
        case 0:
            return "零"; //zero
        case 1:
            return "壹"; //one
        case 2:
            return "貳"; //you get the idea...
        case 3:
            return "叁";
        case 4:
            return "肆";
        case 5:
            return "伍";
        case 6:
            return "陸";
        case 7:
            return "柒";
        case 8:
            return "捌";
        case 9:
            return "玖";
        default:
            return "";
    }
}

private String getThatWord (int index) {
    switch (index) {
        case 0:
            return "";
        case 1:
            return "拾"; //ten
        case 2:
            return "佰"; //hundred
        case 3:
            return "仟"; //thousand
        case 4:
            return "萬"; //ten thousand
        case 5:
            return "億"; //hundred million
        case 6:
            return "兆";//trillion
        default:
            return ""; //I know, Chinese numbers are different.
    }
}

//here is the recursive part
private String convertInteger (String number) {
    if (number.length () < 5) {
        if (number.length () == 1)
            return convertNumber (getDigitFromString (number, 0));
        String finalString = convertNumber (getDigitFromString (number, 0)) +
                getThatWord (number.length () - 1);
        number = number.substring (1);

        if (Integer.parseInt (number) == 0) {
            return finalString;
        }

        for (int i = 0 ; i < number.length () ; i++) {
            if (number.charAt (i) == '0')
                continue;
            return finalString + convertInteger (number.substring (i));
        }
        return null;
    } else {
        int charsToRead = number.length () % 4;
        if (charsToRead == 0) charsToRead = 4;
        String firstPart = number.substring (0, charsToRead);
        String secondPart = number.substring (charsToRead);
        int thatWordIndex = 3 + number.length () / 4;

        if (charsToRead == 4) {
            thatWordIndex--;
        }
        String thatWord = getThatWord (thatWordIndex);

        return convertInteger (firstPart) + thatWord + convertInteger (secondPart);
    }
}

private int getDigitFromString (String str, int index) {
    return Integer.parseInt (Character.toString (str.charAt (index)));
}

如您所见,我有一个递归方法。它转换第一个数字并调用自身来转换其余数字。因此,如果数量非常大,它会自动调用很多次。所以我想问一下,在StackOverflowError发生之前,方法可以调用多少次?

1 个答案:

答案 0 :(得分:1)

递归适用于O(log N)算法,例如快速排序和线性二分搜索。

对于O(N)来说效果不太好,这是你的情况。

根据经验,在这种情况下避免使用递归算法。他们可以始终转换为循环。

您的具体答案:它将从JVM到JVM不等。任何超过20的东西都会有问题。