首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >String.split复制: OutOfBoundsException

String.split复制: OutOfBoundsException
EN

Stack Overflow用户
提问于 2014-09-16 16:29:56
回答 5查看 153关注 0票数 1

对于我的CompSci课程,我们做一个你愿意吗?功能为我们的聊天机器人项目。String.split()方法可以很好地解决这个问题,但是如果没有它我们就能得到加分。我决定通过创建一个复制String.split的方法来实现这一点。

代码语言:javascript
复制
private String[] separate (String phrase, String omit1, String omit2)
{
    int c = 0;

    //gets rid of leading and trailing whitespace, replaces target characters
    //with the # character
    phrase = phrase.trim();
    phrase = phrase.replace(omit1, "#");
    phrase = phrase.replace(omit2, "#");

    //detects the number of phrases to be included in the array
    for (int i = 0; i < phrase.length(); i++)
        if (phrase.charAt(i) == '#')
            c++;

    //creates array list based on number of phrases
    String[] phraseList = new String[c];
    c = 0;

    //builds phrases from characters found between occurrences
    //of the # character
    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase.charAt(i) == '#')
            c++;
        else if (phrase.charAt(i) != '#')
            phraseList[c] += phrase.charAt(i);
    }

    return phraseList;

}

每当我用这种方法时,我就会说:“你想喝茶,吃饼干,还是向上推?”(omit1为",“omit2为”或“)它抛出此异常:

代码语言:javascript
复制
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Magpie.separate(Magpie.java:306)
    at Magpie.getResponse(Magpie.java:44)
    at MagpieRunner.main(MagpieRunner.java:24)

我意识到这与phraseList数组的计数器有关,但到目前为止,我修复它的尝试都没有结果。

有什么帮助吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-09-16 16:32:29

因为如果您甚至有1#,那么您将有2个字符串,所以您需要在创建一个新数组时执行c+1

喜欢

代码语言:javascript
复制
//creates array list based on number of phrases
String[] phraseList = new String[c+1];
c = 0;

您应该使用replaceAll(omit1,"#")而不是replace(omit1,"#") & replace(omit2,"#")

您能提供更多关于空值从哪里来的信息吗?

编辑:

你试过类似的东西吗?

代码语言:javascript
复制
phraseList[0]="";
for(int i = 0; i < phrase.length(); i++)
  {
     if(phrase.charAt(i) == '#')
     {
        c++;
        phraseList[c]="";
     }else if(phrase.charAt(i) != '#')
     {
        phraseList[c] += phrase.charAt(i);
     }
  }
票数 1
EN

Stack Overflow用户

发布于 2014-09-16 16:36:37

把它想象成分隔列表的逗号:

代码语言:javascript
复制
1 , 2 , 3 , 4 , 5 , 6

如果你数一下逗号,你会发现有五个;但是列表中有六个条目。这是因为逗号分隔了条目,但是每个条目都有一个。

或者从栅栏柱和板的角度来考虑:五个柱子,四个面板。

当您创建存储短语的数组时,您需要比有拆分点的条目多一个条目,以确保有空间存储所有短语。

但是,完全避免这种情况并返回一个List<String>而不是String[]会更容易一些。这样,你就不需要事先知道尺寸了。

票数 1
EN

Stack Overflow用户

发布于 2014-09-16 16:40:17

由于其他答案显示了代码的问题所在,下面提供了一种更清晰的方法来分离您可能喜欢的字符串,并且它的行为更像实际的split()方法:

代码语言:javascript
复制
private String[] separate(String phrase, String delim) {
    List<String> tokens = new ArrayList<String>();

    // add delimiter to the end of the string
    // so last token will be included properly
    phrase += delim;

    // start from index of first deliminator
    // i is the index for the deliminator
    // j is the index for the first char of the expression before deliminator
    int i, j = 0;

    // while there are deliminators
    while( (i = phrase.indexOf(delim, j)) != -1)  {
        // obtain the current token from j to deliminator location
        String token = phrase.substring(j, i);
        // trim leading/trailing spaces of the token and make sure it has any chars
        // if it does, add the token to list
        if(token.trim().length() != 0) {
            tokens.add(token);
        }
        // update j to the first character after the deliminator
        j = i + delim.length();
    }

    return tokens.toArray(new String[0]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25873979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档