Getting array index out of bound exception after splitting a string

huangapple go评论55阅读模式
英文:

Getting array index out of bound exception after spliting a string

问题

split字符串后,如果索引的最后一个值存在,那么我就不会收到任何异常

public class Digits {

    public static void main(String[] args) {
        String s = "123;456;;;777;000";
        String field[] = s.split(";");
        System.out.println(field.length);
        System.out.println(field[5]);
    }
}

输出为 ---> s[5] = 000

public class Digits {

    public static void main(String[] args) {
        String s = "123;456;;;;";
        String field[] = s.split(";");
        System.out.println(field.length);
        System.out.println(field[5]);
    }
}

输出为 ----> 数组索引越界异常

我期望得到null值,但它抛出了错误。

英文:

After splitting a string if last value of index is there then I am not getting any exception.

public class Digits {

    public static void main(String[] args) {
        String s = "123;456;;;777;000";
        String field[] = s.split(";");
        System.out.println(field.length);
        System.out.println(field[5]);
    }
}

Output is ---> s[5] = 000

public class Digits {

    public static void main(String[] args) {
        String s = "123;456;;;;";
        String field[] = s.split(";");
        System.out.println(field.length);
        System.out.println(field[5]);
    }
}

Output is----> ArrayindexoutofBoundException

I am expecting null value but it is throwing error.

答案1

得分: 1

如果您使用替代方法,它不会抛出错误

String s = "123;456;;;;";
String field[] = s.split(";", -1);
System.out.println(field.length);
System.out.println(field[5]);

根据单参数版本

因此,尾随的空字符串不会包含在结果数组中。

但是使用两个参数的版本

如果n为非正数,则将尽可能多地应用模式。

英文:

If you use the alternative method, it will not throw an error

String s = "123;456;;;;";
String field[] = s.split(";", -1);
System.out.println(field.length);
System.out.println(field[5]);

As per the single parameter version

> Trailing empty strings are therefore not included in the resulting
> array.

However with the 2 parameter version

> If n is non-positive then the pattern will be applied as many times as possible

答案2

得分: -1

当你使用String field[] = s.split("";");时,实际调用的方法是split("";"", 0)

public String[] split(String regex) {
    return split(regex, 0);
}

// @param  regex : 分隔正则表达式
// @param  limit : 结果阈值,如上所述
public String[] split(String regex, int limit) {
    // ...
}

`limit=0``结果列表的尾元素等于0`,`split`方法通过以下方式构造结果移除空元素

// 构造结果
int resultSize = list.size();
if (limit == 0) {
    while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
        resultSize--;
    }
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);

这就是为什么`s = ""123;456;;;777;000""`不会引发异常`s = ""123;456;;;;""`会引发ArrayIndexOutOfBoundsException的原因

总之如果你想得到`""`你可以赋值`limit = -1或其他负数`(只要你不想限制`split`的范围),例如

```java
String field[] = s.split("";"", -1);
String field[] = s.split("";"", -2);
英文:

when you use String field[] = s.split(";");, the actual calling method is split(";",0).

 public String[] split(String regex) {
    return split(regex, 0); 
 }

 // @param  regex : the delimiting regular expression
 // @param  limit : the result threshold, as described above
 public String[] split(String regex, int limit) {
	...
 }

when limit=0 and the trailing element of the result list is equal to 0, method split constructs the result by removing the empty elements as follows:

// Construct result
int resultSize = list.size();
if (limit == 0) {
    while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
        resultSize--;
    }
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);

This is the reason for s = "123;456;;;777;000" not getting any exception, but s = "123;456;;;;" get ArrayindexoutofBoundException.

In conclusion, if you want get "" value, you can assgin limit = -1 or other inegative number (Provided you don't want to limit the scope of the split), for example:

String field[] = s.split(";", -1);
String field[] = s.split(";", -2);

huangapple
  • 本文由 发表于 2023年4月17日 13:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定