0

我正在编写一个函数,其中需要使用元素创建输入序列的子字符串Integer List(例如 for string s1,子字符串可以是s1.substring(iList.get(i), iList.get(i+1)+1))。我已经设置了一个 if 语句来检查字符串长度是否总是大于(i+1)列表的元素(这将是子字符串中的终点)。我仍然间歇性地得到 String Out of Bounds 异常。我的输入字符串通常是 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;
}

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

4

1 回答 1

2

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

if (str != null && from >= 0 && to >= from && to <= str.length()) {
  // then it's safe
  String sub = str.substring(from, to);
}
于 2013-10-28T21:58:15.773 回答