java - StackOverflowError 之前执行了多少次迭代

标签 java android recursion stack-overflow

<分区>

我目前正在编写一个 Android 支票编写器程序,它获取用户输入(阿拉伯数字)并将其转换为支票样式的数字。比如说四千五 block 钱,只有一分钱,诸如此类。英语部分有点简单,我完成了。现在我在不同的 Activity 中写中文版。这次我用了递归算法,因为中文数字写法很不一样。这是算法:(评论是翻译)

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 发生之前,一个方法可以调用多少次?

最佳答案

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

它对 O(N) 的效果要差得多,这是你的情况。

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

您的具体答案:它会因 JVM 的不同而不同。任何超过 20 的值都是有问题的。

关于java - StackOverflowError 之前执行了多少次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066964/

相关文章:

java - Android java if 语句不适用于 OnClickListener

android - 所有使用 hdpi xml 文件的设备

swift - 使用取决于元素类型的递归属性/方法扩展 Collection

java - 通过 Restful Webservice 写入 JSONobject

java - 我有 Spring Boot 应用程序,但出现以下错误

java - 从 Android Websocket 客户端发送消息

Android 位图 setPixel 无法正常工作? (设置值,然后读取不同的值)

java - 在android中将字符串转换为日期 "2016-01-28T12:08:47.676706-05:00"

javascript - javascript中的递归函数问题

php - 如何在PHP的递归函数中使用return