[英]How do I delete elements from a list based on the boolean values in my list instead of deleting them by index?
I have a list of elements that contains booleans and strings.我有一个包含布尔值和字符串的元素列表。 I have the list output matching what I want, however I need to make it able to delete based on the leading boolean value instead of index.我有列表 output 匹配我想要的,但是我需要使它能够根据领先的 boolean 值而不是索引来删除。 This is the code I have now这是我现在拥有的代码
public class collectionsPractice {
public static void main(String[] args) {
ArrayList fruits = new ArrayList();
fruits.add(false);
fruits.add("Strawberry");
fruits.add("Pineapple");
fruits.add(true);
fruits.add("Watermelon");
fruits.add("Lemon");
fruits.add(false);
fruits.add("Cantaloupe");
fruits.add("Grapes");
fruits.add("Coconut");
fruits.remove(4);
fruits.remove(4);
System.out.println(fruits);
}
}
The list before the removal looks like this:删除前的列表如下所示:
[false, Strawberry, Pineapple, true, Watermelon, Lemon, false, Cantaloupe, Grapes, Coconut]
After the remove calls it looks like this:在 remove 调用之后,它看起来像这样:
[false, Strawberry, Pineapple, true, false, Cantaloupe, Grapes, Coconut]
I want the strings after every true boolean to be deleted.我希望删除每个真正的 boolean 之后的字符串。 (I know the remove statements look weird, but I have to call them twice because after the first call the index of "Lemon" shifts down by one). (我知道删除语句看起来很奇怪,但我必须调用它们两次,因为在第一次调用之后,“Lemon”的索引向下移动一个)。
So instead of having the remove at some index, I want to loop through the list and see if the boolean value in the list is true or false.因此,我不想在某个索引处删除,而是想遍历列表并查看列表中的 boolean 值是真还是假。 Then if false I want to keep going through the list.然后,如果为假,我想继续浏览列表。 However, if it is true, I want to delete the strings in between that boolean and the next boolean.但是,如果这是真的,我想删除 boolean 和下一个 boolean 之间的字符串。 The output should look the same as the output I have with the two remove statements, I just want to be able to do that without having to know how many objects or items are in the list. output 看起来应该与我使用两个删除语句的 output 相同,我只想能够做到这一点而不必知道列表中有多少对象或项目。 Is there a way to do this?有没有办法做到这一点? If any clarification is needed, please let me know in the comments.如果需要任何澄清,请在评论中告诉我。
You may traverse list from the end to avoid mutation of the "tail", something like:您可以从末尾遍历列表以避免“尾巴”的突变,例如:
public static void main(String[] args) {
ArrayList fruits = new ArrayList();
fruits.add(false);
fruits.add("Strawberry");
fruits.add("Pineapple");
fruits.add(true);
fruits.add("Watermelon");
fruits.add("Lemon");
fruits.add(true);
fruits.add(false);
fruits.add("Cantaloupe");
fruits.add("Grapes");
fruits.add(true);
fruits.add("Coconut");
int end = fruits.size();
for (int i = end - 1; i >= 0; i--) {
if (Boolean.TRUE.equals(fruits.get(i))) {
fruits.subList(i + 1, end).clear();
end = i;
} else if (Boolean.FALSE.equals(fruits.get(i))) {
end = i;
}
}
System.out.println(fruits);
}
Here's a possible implementation:这是一个可能的实现:
ArrayList fruits = new ArrayList();
fruits.add(false);
fruits.add("Strawberry");
fruits.add("Pineapple");
fruits.add(true);
fruits.add("Watermelon");
fruits.add("Lemon");
fruits.add(false);
fruits.add("Cantaloupe");
fruits.add("Grapes");
fruits.add("Coconut");
fruits.add("Lemon");
System.out.println(fruits); //before
ArrayList toRemove = new ArrayList();
boolean deletionFlag = false;
for(int i = 0; i < fruits.size(); i++){
Object obj = fruits.get(i);
if (obj instanceof Boolean){
deletionFlag = (boolean)obj;
}else{
if(deletionFlag) toRemove.add(obj);
}
}
fruits.removeAll(toRemove);
System.out.println(fruits); //after
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.