[英]Two arrays comparison with three for loops
My title isn't the best but i haven't any other ideas. 我的头衔不是最好的,但我没有其他想法。 I have one
List
of objects with a List
categories parameter and one String
. 我有一个
List
与对象的List
类参数和一个String
。 The String is something like: "action,azione adventure,avventura horror sport "
. 字符串类似于:
"action,azione adventure,avventura horror sport "
。 I have to split it with spaces to obtain an array of strings like: ["action,azione", "adventure,avventura", "horror", "sport"]
. 我必须用空格将其分开,以获得一个字符串数组,例如:
["action,azione", "adventure,avventura", "horror", "sport"]
。 I have to remove an item from the List of objects if his List categories item isn't contained in the array of String. 如果String数组中未包含他的List Categories项目,则必须从对象列表中删除该项目。 I know that it could sound tricky so i'll make some example:
我知道这听起来很棘手,所以我举个例子:
Array: ["action,azione", "adventure,avventura", "horror", "sport"]
数组:
["action,azione", "adventure,avventura", "horror", "sport"]
List categories (of the actual List object): ["action", "adventure", "horror", "comic", "sport"]
remain 列表类别(实际的List对象的类别): 保留
["action", "adventure", "horror", "comic", "sport"]
Array: ["action,azione", "adventure,avventura", "horror", "sport"]
数组:
["action,azione", "adventure,avventura", "horror", "sport"]
List categories (of the actual List object): ["azione", "horror", "comico", "sport"]
delete because adventure categories isn't there (实际List对象的)列表类别:
["azione", "horror", "comico", "sport"]
删除,因为没有冒险类别
Here is my try: 这是我的尝试:
listaManga.getManga()
is the List of objects listaManga.getManga()
是对象列表
listaManga.getManga().get(index).getC()
is the List of categories of that object listaManga.getManga().get(index).getC()
是该对象的类别列表
String[] categories
is the String splitted in spaces String[] categories
是用空格分隔的String
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;
}
}
}
}
It throws IndexOutOfBoundsException
on j
value. 它在
j
值上引发IndexOutOfBoundsException
。
See if the following works. 查看以下是否有效。
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;
//}
}
}
}
I finally solved it following the inverse loop cycle idea but i had to change the code of muasif80 cause his code take to an issue: It can get right categories only if in listaManga.getManga().get(i).getC()
there aren't more categories. 我终于按照逆循环周期的思想解决了它,但是我不得不更改muasif80的代码,导致他的代码出现问题:只有在
listaManga.getManga().get(i).getC()
那里,它才能获取正确的类别listaManga.getManga().get(i).getC()
没有更多类别。 I'll make an example: if i have ["comic", "adventure"]
as String[] categories
, it will find all list item with ["comic", "adventure"]
categories but not also item with more other categories like: ["comic", "adventure", "horror"]
. 我举一个例子:如果我将
["comic", "adventure"]
作为String[] categories
,它将找到所有具有["comic", "adventure"]
类别的列表项,但不会找到其他类别的项像: ["comic", "adventure", "horror"]
。
I did it setting a new String to check every possibility: 我设置了一个新的String来检查所有可能性:
String is_contained = "";
String[] categories = MainActivity.categories.split(" ");
for (int i = (listaManga.getManga().size() - 1); i >= 0; i--) {
if(listaManga.getManga().get(i).getC().size()==0){
listaManga.getManga().remove(i);
} else {
for (int j = (categories.length - 1); j >= 0; j--) {
is_contained = "";
for (int z = (listaManga.getManga().get(i).getC().size() - 1); z >= 0; z--) {
if (!categories[j].toLowerCase().contains((String.valueOf(listaManga.getManga().get(i).getC().get(z))).toLowerCase())) {
is_contained += "a";
} else {
is_contained += "b";
}
}
if (!is_contained.toLowerCase().contains(("b").toLowerCase())) {
listaManga.getManga().remove(i);
break;
}
}
}
}
Ps Also if this is the most correct answer, i'll accept the one of muasif80 for all the time that he dedicated to me in chat! 附:如果这是最正确的答案,我将一直接受他在聊天中一直献给我的muasif80之一!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.