我的标题不是最好的,但我没有任何其他想法。我有一个List
具有 List
的对象类别参数和一 String
。该字符串类似于:"action,azione adventure,avventura horror sport "
。我必须用空格将其分割以获得一个字符串数组,例如: ["action,azione", "adventure,avventura", "horror", "sport"]
。
如果他的列表类别项目不包含在字符串数组中,我必须从对象列表中删除该项目。
我知道这听起来很棘手,所以我举一些例子:
数组:["action,azione", "adventure,avventura", "horror", "sport"]
列表类别(实际列表对象的):["action", "adventure", "horror", "comic", "sport"]
留下来
数组:["action,azione", "adventure,avventura", "horror", "sport"]
列表类别(实际列表对象的):["azione", "horror", "comico", "sport"]
删除,因为冒险类别不存在
这是我的尝试:
listaManga.getManga()
是对象列表
listaManga.getManga().get(index).getC()
是该对象的类别列表
String[] categories
是以空格分割的字符串
String[] categories = MainActivity.categories.split(" ");
for (int i = 0; i < listaManga.getManga().size(); i++) {
for (int j = 0; j < categories.length; j++) {
for (int z = 0; z < listaManga.getManga().get(i).getC().size(); z++) {
if (!categories[j].contains(String.valueOf(listaManga.getManga().get(i).getC().get(z)))) {
listaManga.getManga().remove(i);
break;
}
}
}
}
它抛出IndexOutOfBoundsException
上j
值。
最佳答案
看看以下是否有效。
String[] categories = MainActivity.categories.split(" ");
boolean found = false;
for (int i = (listaManga.getManga().size() - 1); i >= 0; i--) {
found = false;
for (int j = (categories.length - 1); j >= 0; j--) {
for (int z = (listaManga.getManga().get(i).getC().size() - 1); z >= 0; z--) {
if (!categories[j].contains(String.valueOf(listaManga.getManga().get(i).getC().get(z)))) {
listaManga.getManga().remove(i);
// Either do this
// i = i - 1;
// or put a flag here that is
// found == true;
break;
}
//if(found == true){
// break;
//}
}
}
}
关于java - 使用三个 for 循环比较两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032327/