简体   繁体   English

递归函数中的字符串操作导致堆栈溢出

[英]String operation in recursive function causing stackoverflow

I am building recursive function that go through a million line or so, I get stackOverFlow from this function during execution. 我正在构建经过一百万行左右的递归函数,我在执行过程中从此函数获取stackOverFlow。

protected String[] getConnectedCities(String line) {
    return line.trim().toLowerCase().replace(DELIMITER + " ", DELIMITER)
            .split(DELIMITER);
}

This is the full code: 这是完整的代码:

    protected final Map<String, City> processLine(
        final Map<String, City> dataMap) {
    try {
        String line = "";
        if ((line = bReader.readLine()) == null) {
            return dataMap;
        }
        // Check if direct relation can be found
        String connectedCities[] = parseLine(line);
        line = null;
        saveConnection(dataMap, connectedCities);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return processLine(dataMap);
}

I am not sure what am I doing wrong, I think it is related to the String line but am not quite sure what is it. 我不确定我在做什么错,我认为这与String行有关,但我不太确定这是什么。

Thanks. 谢谢。

The last thing you do, you're calling processLine agan. 您要做的最后一件事,就是调用processLine agan。 This will have as many levels of recusion as you have lines in file, until return statement above bails out. 这将具有与文件中的行数一样多的级别的限制,直到上面的return语句失败为止。 This is technically tail recursion, but Java may not understand that. 从技术上讲,这是尾部递归,但是Java可能不理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM