从字符串中删除某些信息时数组越界异常
Array out of bound exception while deleting certain information from a String
我有一个包含 String
的 ArrayList
。我想遍历这个 ArrayList
,获取所有 String
并查找它们是否包含特定字符集,然后将其删除。
我的意思的例子:在 String
"Hi, my name is Bobby" 中,我想删除 "Hi, my name is " 并只保留 "Bobby".
这是我的函数:
private ArrayList<String> epurateInformation(ArrayList<String> list) {
String[] potentialThreat = {"Pret : ", "Nom de l'Album : ", "Style : ", "Date : ", "Nom de la piste : ", "Propriétaire : ", "Information : "};
for(String s : list) {
for(int i = 0; i <= potentialThreat.length; i++) {
System.out.println(potentialThreat[i]);
//The error is right below here
if(s.contains(potentialThreat[i])) {
s.replace(potentialThreat[i], "");
}
cdInformation.add(s);
}
}
return list;
}
它给了我一个java.lang.ArrayIndexOutOfBoundsException
。
我试图将 potentialThreat.length 替换为固定的 int
(按 7、6 和 1),但它仍然给我这个错误。
如果您需要更多信息,请告诉我。如果您对如何 运行 有其他想法,也请告诉我。谢谢
数组是从零开始的,你需要 < not <= 所以:
private ArrayList<String> epurateInformation(ArrayList<String> list) {
String[] potentialThreat = {"Pret : ", "Nom de l'Album : ", "Style : ", "Date : ", "Nom de la piste : ", "Propriétaire : ", "Information : "};
for(String s : list) {
for(int i = 0; i < potentialThreat.length; i++) {
System.out.println(potentialThreat[i]);
//The error is right below here
if(s.contains(potentialThreat[i])) {
s.replace(potentialThreat[i], "");
}
cdInformation.add(s);
}
}
return list;
}
我有一个包含 String
的 ArrayList
。我想遍历这个 ArrayList
,获取所有 String
并查找它们是否包含特定字符集,然后将其删除。
我的意思的例子:在 String
"Hi, my name is Bobby" 中,我想删除 "Hi, my name is " 并只保留 "Bobby".
这是我的函数:
private ArrayList<String> epurateInformation(ArrayList<String> list) {
String[] potentialThreat = {"Pret : ", "Nom de l'Album : ", "Style : ", "Date : ", "Nom de la piste : ", "Propriétaire : ", "Information : "};
for(String s : list) {
for(int i = 0; i <= potentialThreat.length; i++) {
System.out.println(potentialThreat[i]);
//The error is right below here
if(s.contains(potentialThreat[i])) {
s.replace(potentialThreat[i], "");
}
cdInformation.add(s);
}
}
return list;
}
它给了我一个java.lang.ArrayIndexOutOfBoundsException
。
我试图将 potentialThreat.length 替换为固定的 int
(按 7、6 和 1),但它仍然给我这个错误。
如果您需要更多信息,请告诉我。如果您对如何 运行 有其他想法,也请告诉我。谢谢
数组是从零开始的,你需要 < not <= 所以:
private ArrayList<String> epurateInformation(ArrayList<String> list) {
String[] potentialThreat = {"Pret : ", "Nom de l'Album : ", "Style : ", "Date : ", "Nom de la piste : ", "Propriétaire : ", "Information : "};
for(String s : list) {
for(int i = 0; i < potentialThreat.length; i++) {
System.out.println(potentialThreat[i]);
//The error is right below here
if(s.contains(potentialThreat[i])) {
s.replace(potentialThreat[i], "");
}
cdInformation.add(s);
}
}
return list;
}