Java 在android中拆分字符串时获取数组索引越界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18179895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:04:08  来源:igfitidea点击:

Getting array index out of bounds when splitting a string in android

javaandroid

提问by Amrutha

I am working on android. I have a string like below.

我在安卓上工作。我有一个像下面这样的字符串。

String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@......"

String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@......"

I am splitting the above string with "@" using String[] str1 = str.split("[@]");so that the final results are

我正在使用“@”分割上面的字符串,String[] str1 = str.split("[@]");以便最终结果是

str1[0] = test1|test2|test3

str1[0] = test1|test2|test3

str1[1] = test4|test5|test6|test7

str1[1] = test4|test5|test6|test7

...

...

The code snippet I used

我使用的代码片段

   for (int x = 1; x < str1.length; x++) {
    String[] st1 = str1 [x].trim().split("\|");
    System.out.println("first" + st1[0].trim());
    System.out.println("second" + st1[1].trim());
    System.out.println("third" + st1[2].trim());
    System.out.println("fourth" + st1[3].trim());  

   List1.add(st1[0].trim());
   List2.add(st1[1].trim());
   List3.add(st1[2].trim());
   List4.add(st1[3].trim());

    }

In the above for loop, when the loop starts from x=2 it is working fine. But if I give x=1 then it is throwing array index out of bounds exception at "System.out.println("fourth" + st1[3].trim());". Is because str1[0] consists of only 3 items whereas the remaining consists of 4 items. So now I am unable to get the fourth value after splitting with "|". Please tell me how to get the fourth value. Thanks in advance.

在上面的 for 循环中,当循环从 x=2 开始时,它工作正常。但是,如果我给 x=1,那么它会在“System.out.println("fourth" + st1[3].trim());”处抛出数组索引越界异常。是因为 str1[0] 仅包含 3 个项目,而其余的包含 4 个项目。所以现在我无法在用“|”分割后获得第四个值。请告诉我如何获得第四个值。提前致谢。

回答by Umer Farooq

You have @in your string I don't see [@]any where, so why are you splitting the string with this token.

@在你的字符串中我没有看到[@]任何地方,那么你为什么要用这个标记拆分字符串。

Change

改变

String[] str1 = str.split("[@]")

to

String[] str1 = str.split("@")

For solving indexOutOfBounds:

为了解决 indexOutOfBounds:

for (int x = 1; x < str1.length; x++) {
  String[] st1 = str1 [x].trim().split("\|");
  for (int j = 0; j < st1.length; j++)
  {
      System.out.println( j "= " + st1[j].trim());
  }

}

回答by Bhoomika Brahmbhatt

for (int x = 1; x < str1.length; x++) {
String[] st1 = str1 [x].trim().split("\|");
System.out.println("first" + st1[0].trim());
System.out.println("second" + st1[1].trim());
System.out.println("third" + st1[2].trim());
System.out.println("fourth" + st1[3].trim());

}

Change to:

改成:

for (int x = 0; x < str1.length; x++) {
String[] st1 = str1 [x].trim().split("\|");
for (int y = 0; y < st1.length; y++)
{
System.out.println("value at "+String.ValueOf(y)+": " + st1[y].trim());
}

}

回答by Master

ArrayIndexOutOFBound is coming because you are accessing the forth value you can get by following two ways,

ArrayIndexOutOFBound 即将到来,因为您正在访问可以通过以下两种方式获得的第四个值,

String[] str1 = str.split("@");
        for (int x = 1; x < str1.length; x++) {
            String[] st1 = str1[x].trim().split("\|");
            for (int j = 0; j < st1.length; j++) {
                System.out.println(st1[j]);

            }

        }

or

或者

String[] str1 = str.split("@");
        for (int x = 1; x < str1.length; x++) {
            String[] st1 = str1[x].trim().split("\|");
            /*for (int j = 0; j < st1.length; j++) {
                System.out.println(st1[j]);

            }*/
            if(st1.length> 0 )
                System.out.println("first" + st1[0].trim());
            if(st1.length> 1 )
                System.out.println("second" + st1[1].trim());
            if(st1.length> 2 )
                System.out.println("third" + st1[2].trim());
            if(st1.length> 3 )
                System.out.println("fourth" + st1[3].trim());

        }

回答by Gunaseelan

String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@";
        String[] str1 = str.split("[@]");

        for (int x = 0; x < str1.length; x++) {
            String[] str2 = str1[x].trim().split("\|");
            for (int y = 0; y < str2.length; y++) {
                System.out.println(str2[y].trim());
            }
        }

Output:

输出:

test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test14
test15

Hope this will help you.

希望这会帮助你。

回答by Phuong Luu Hoang

You have IndexOutOfBoundException because the first substring (str1[0]) only has 3 items and you tried to access the 4th item. This code snippet should work.

您有 IndexOutOfBoundException,因为第一个子字符串 (str1[0]) 只有 3 个项目,而您试图访问第 4 个项目。此代码片段应该可以工作。

String[] str1 = str.split( "[@]" );
List<String> list = new ArrayList<String>();
for ( int x = 1; x < str1.length; x++ )
{
    String[] st1 = str1[ x ].trim().split( "\|" );
    for ( int index = 0; index < st1.length; index++ )
    {
        String value = st1[ index ].trim();
        System.out.println( "value in index " + index + "=" + value );
        list.add( value );
    }
 }