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 に答える