【问题标题】:String Out of Bounds Exception when creating subStrings using Integer List Elements使用整数列表元素创建子字符串时出现字符串越界异常
【发布时间】:2013-10-28 21:56:14
【问题描述】:

我正在编写一个函数,其中需要使用 Integer List 的元素创建输入序列的子字符串(例如,对于 string s1,子字符串可以是 s1.substring(iList.get(i), iList.get(i+1)+1))。我已经设置了一个 if 语句来检查字符串长度是否始终大于列表的(i+1) 元素(这将是子字符串中的终点)。我仍然间歇性地收到字符串越界异常。我的输入字符串通常是 80-90K 字符,错误似乎发生 70-80%。由于错误的间歇性,我发现很难排除故障。下面是我的代码:

    public static List<Integer> finalCPGIslands(List<Integer> iList,
        String iSeq, int width) {
    // Declare output list that contains final list of start and end
    // intervals
    List<Integer> oList = new ArrayList<Integer>();
    // Add the first two elements anyways
    //if (cpgCriteriaCheck(iSeq.substring(iList.get(0), iList.get(1)+1))) {
        oList.add(iList.get(0));
        oList.add(iList.get(1));
    //}

    if (iList.size() > 2) {
        for (int i = 0; i < iList.size()-1; i += 2) {
            // The below IF is attempted to ensure that substring is always
            // valid
            if (iSeq.length()-1 > iList.get(i + 1)) {
                // While creating the substring in next line, I get String
                // index out of range: -9
                String testSeq = iSeq.substring(iList.get(i),
                        iList.get(i + 1) + 1);
                boolean check = cpgCriteriaCheck(testSeq);
                if (check) {
                    // If condition is met, add the indexes to the final
                    // list
                    oList.add(iList.get(i));
                    oList.add(iList.get(i + 1));
                }
                // If condition is not met, start removing one character at
                // a time until condition is met
                else {

                    int counter = 0;
                    int currentSequenceLength = testSeq.length();
                    String newTestSeq = null;
                    while (counter <= currentSequenceLength) {
                        counter++;
                        if (testSeq.length() > 2) {
                            newTestSeq = testSeq.substring(1,
                                    testSeq.length() - 1);
                            testSeq = newTestSeq;
                            if (newTestSeq.length() < width) {
                                counter = currentSequenceLength + 1;
                            } else {
                                boolean checkAgain = cpgCriteriaCheck(newTestSeq);
                                // If condition met, add the item to list
                                // and exit
                                if (checkAgain) {
                                    oList.add(iList.get(i) + counter);
                                    oList.add(iList.get(i + 1) - counter);
                                    counter = currentSequenceLength + 1;
                                }

                            } // End of Else
                        } // End of IF

                    } // End of While
                } // End of Else
            }

        } // End of For
    } // End of Else
    return oList;
}

我在 cmets 中提到过出现越界错误。我是否错过了在执行子字符串之前需要执行的一些检查?我正在检查以确保字符串长度大于列表元素的值的 IF 语句不应该涵盖任何字符串越界异常吗?

【问题讨论】:

  • String testSeq = iSeq.substring(iList.get(i), iList.get(i + 1) + 1); --> 尝试打印/调试并查看从 iList.get(i)iList.get(i+1) 返回的值 - 这可能不是您所期望的......

标签: java


【解决方案1】:

无论何时你做.substring(from, to),理想情况下你应该这样做:

if (str != null && from >= 0 && to >= from && to <= str.length()) {
  // then it's safe
  String sub = str.substring(from, to);
}

【讨论】:

  • to &lt;= str.length() 因为String#substring(int, int) 中的endIndex 是独占的。
  • 真的,我的错。已编辑。
  • 感谢 iluxa 提供答案。这涵盖了所有错误情况。
猜你喜欢
  • 2014-07-31
  • 1970-01-01
  • 2015-07-13
  • 2021-04-17
  • 1970-01-01
  • 2019-08-01
  • 2013-12-08
  • 2014-10-16
  • 2017-03-22
相关资源
最近更新 更多