LastIndexOf和java.lang.IndexOutOfBoundsException

By simon at 2018-02-07 • 0人收藏 • 55人看过

我有一个强大的CCAATA CCGT,我试图得到固定长度,n, 连续的子序列。那我想弄点东西喜欢这个: 该字符串中每个子序列的索引。 0-3,1-4,2-5等

0 thru 3 : CCAA 
1 thru 4 : CAAT 
2 thru 5 : AATA 
3 thru 6 : ATAC 
4 thru 7 : TACC 
5 thru 8 : ACCG 
6 thru 9 : CCGT 
清单大小是7.在这里,我是罗选择通过列表并获取索引& lastIndexOf。之后,3 thru 6 : ATAC,我得到了

线程“主”java.lang.IndexOut异常OfBoundsException:索引:7, 大小:7

for (int i = 0; i < list.size(); i++) {
            System.out.println(ss.indexOf(list.get(i)) 
             + " thru " + ss.lastIndexOf(list.get(i + n - 1)) + " : " 
            + list.get(i));
演示:
import java.util.ArrayList;

public class Subsequences {

    public static void main(String[] args) {

        String s = "CCAATA CCGT";
        ArrayList<String> list = new ArrayList<String>(); // list of subsequence

        int n = 4; // subsequences of length

        String ss = s.replaceAll("\\s+", "");
        String substr = null;

        for (int i = 0; i <= ss.length() - n; i++) {
            substr = ss.substring(i, i + n);
            list.add(substr);
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.println(ss.indexOf(list.get(i)) 
             + " thru " + ss.lastIndexOf(list.get(i + n - 1)) + " : " 
            + list.get(i));

        }
    }
}
任何提示?

6 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

你不需要添加n到lastIndexOf,因为你已经分开了 substring由4.每个条目

 for (int i = 0; i < list.size(); i++) {
        System.out.println(ss.indexOf(list.get(i))
                + " thru " + (ss.lastIndexOf(list.get(i)) + n - 1) + " : "
                + list.get(i));

    }
7由4个字符组成。更改 你的索引检查到这一点
(ss.lastIndexOf(list.get(i)) + n - 1)
最后它看起来像这样
 for (int i = 0; i < list.size(); i++) {
        System.out.println(ss.indexOf(list.get(i))
                + " thru " + (ss.lastIndexOf(list.get(i)) + n - 1) + " : "
                + list.get(i));

    }
输出:
0 thru 3 : CCAA   
1 thru 4 : CAAT   
2 thru 5 : AATA   
3 thru 6 : ATAC   
4 thru 7 : TACC   
5 thru 8 : ACCG  
6 thru 9 : CCGT   

2018-02-07   #2

我相信你的问题是在list.get(i + n - 1)。你目前正在迭代 每个子序列的start的范围是0至list.size() - 1.的 最后一个有意义的子序列就是n个字符 list.size() - n通过h list.size() - 1。

for (int i = 0; i < list.size() - n; i++) {
    System.out.println(ss.indexOf(list.get(i)) 
        + " thru " + ss.lastIndexOf(list.get(i + n - 1)) + " : " 
        + list.get(i));

    }

2018-02-07   #3

删除所有空白,循环:

String data = "CCAATA CCGT";
String replaced = data.replaceAll("\\s", "");
for (int i = 0; i < replaced.length() - 4 + 1; i++) {
    System.out.println(replaced.subSequence(i, i + 4));
}
输出:
CCAA
CAAT
AATA
ATAC
TACC
ACCG
CCGT

2018-02-07   #4

你不需要添加n到lastIndexOf,因为你已经分开了 substring由4.每个条目

 for (int i = 0; i < list.size(); i++) {
        System.out.println(ss.indexOf(list.get(i))
                + " thru " + (ss.lastIndexOf(list.get(i)) + n - 1) + " : "
                + list.get(i));

    }
7由4个字符组成。更改 你的索引检查到这一点
(ss.lastIndexOf(list.get(i)) + n - 1)
最后它看起来像这样
 for (int i = 0; i < list.size(); i++) {
        System.out.println(ss.indexOf(list.get(i))
                + " thru " + (ss.lastIndexOf(list.get(i)) + n - 1) + " : "
                + list.get(i));

    }
输出:
0 thru 3 : CCAA   
1 thru 4 : CAAT   
2 thru 5 : AATA   
3 thru 6 : ATAC   
4 thru 7 : TACC   
5 thru 8 : ACCG  
6 thru 9 : CCGT   

2018-02-07   #5

在你的循环中

for (int i = 0; i < list.size(); i++) { 
   System.out.println(ss.indexOf(list.get(i)) 
   + " thru " + ss.lastIndexOf(list.get(i + n - 1))
   + " : " + list.get(i));
}
当你做list.get(i + n - 1)和你的i是4时,结果是 成瘾将是4 + 4 - 1 = 7而你无法获得列表的成员 相同或更大的list.size()索引,所以系统抛出异常 至h大大提高你期望的结果,你可以做这样的事情:
import java.util.ArrayList;

public class Subsequences {

public static void main(String[] args) {

    String s = "CCAATA CCGT";
    ArrayList<String> list = new ArrayList<String>(); // list of subsequence

    int n = 4; // subsequences of length

    String ss = s.replaceAll("\\s+", "");
    String substr = null;

    for (int i = 0; i <= ss.length() - n; i++) {
        substr = ss.substring(i, i + n);
        list.add(substr);
    }

    // --------Here the edits-------
    for (int i = 0; i < list.size(); i++) 
        System.println(i + " thru " + (i+n-1) + " : " + list.get(i))
    // -----------------------------

}
}

2018-02-07   #6

你也可以用一个简单的正则表达式来做到这一点。删除空格并运行 这个正则表达式:

(?=(.{4}))
样品:
package com.see;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    private static final String TEST_STR = "CCAATA CCGT";

    public ArrayList<String> getMatchedStrings(String input) {
        ArrayList<String> matches = new ArrayList<String>();
        input = input.replaceAll("\\s", "");

        Pattern pattern = Pattern.compile("(?=(.{4}))");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find())
            matches.add(matcher.group(1));

        return matches;
    }

    public static void main(String[] args) {
        RegexTest rt = new RegexTest();
        for (String string : rt.getMatchedStrings(TEST_STR)) {
            System.out.println(string);
        }
    }
}

登录后方可回帖

Loading...