gpt4 book ai didi

java - 无法处理 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 19:35:22 24 4
gpt4 key购买 nike

我想分割字符串并将 token 分配给不同的字段,如果 token 大小不符合预期,则会抛出ArrayIndexOutOfBoundsException。当数据符合预期时,它显示空值。

示例代码:

public class Test {
public static void main(final String[] args) {
List<String> firstNames = new ArrayList<>();
List<String> lastNames = new ArrayList<>();
List<String> useCaseIds = new ArrayList<>();
List<String> partner = new ArrayList<>();
List<String> comments = new ArrayList<>();

String s = "Annie-Marie--Joe"; // -NoData";
// final String s = "Annie-Marie-83B782-Joe-NoData"; //showing empty value when printing the list.
String[] tokens = s.split("-");

switch (tokens.length) {
case 1:
firstNames.add(tokens[0]);
case 2:
lastNames.add(tokens[1]);
case 3:
useCaseIds.add(tokens[2]);
case 4:
partner.add(tokens[3]);
case 5:
comments.add(tokens[4]);
default:
break;
}

System.out.println("firstNames " + firstNames);
System.out.println("lastNames " + lastNames);
System.out.println("useCaseIds " + useCaseIds);
System.out.println("partner " + partner);
System.out.println("comments " + comments);
}

}

输出:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

--编辑--修改代码如下所示。

final String s = "Annie-Marie-83B782-Joe"; // showing empty value when
// printing the list.
final String[] tokens = s.split("-");

switch (tokens.length) {
case 1:
firstNames.add(tokens[0]);
break;
case 2:
lastNames.add(tokens[1]);
break;
case 3:
useCaseIds.add(tokens[2]);
break;
case 4:
partner.add(tokens[3]);
break;
case 5:
comments.add(tokens[4]);
break;
default:
break;
}

System.out.println("firstNames " + firstNames);
System.out.println("lastNames " + lastNames);
System.out.println("useCaseIds " + useCaseIds);
System.out.println("partner " + partner);
System.out.println("comments " + comments);

}

输出:

firstNames []
lastNames []
useCaseIds []
partner [Joe]
comments []

为什么在我的上述场景中,名字、姓氏...都是空白。

最佳答案

您必须在棘手的 switch 语句中使用不同的顺序:

import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(final String[] args) {
List<String> firstNames = new ArrayList<>();
List<String> lastNames = new ArrayList<>();
List<String> useCaseIds = new ArrayList<>();
List<String> partner = new ArrayList<>();
List<String> comments = new ArrayList<>();

//String s = "Annie-Marie--Joe"; // -NoData";
final String s = "Annie-Marie-83B782-Joe-NoData"; //showing empty value when printing the list.
String[] tokens = s.split("-");

switch (tokens.length) {
case 5:
comments.add(tokens[4]);
case 4:
partner.add(tokens[3]);
case 3:
useCaseIds.add(tokens[2]);
case 2:
lastNames.add(tokens[1]);
case 1:
firstNames.add(tokens[0]);
default:
break;
}

System.out.println("firstNames " + firstNames);
System.out.println("lastNames " + lastNames);
System.out.println("useCaseIds " + useCaseIds);
System.out.println("partner " + partner);
System.out.println("comments " + comments);
}

}

jdoodle 上测试

关于java - 无法处理 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com