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. 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:
[false, Strawberry, Pineapple, true, false, Cantaloupe, Grapes, Coconut]
I want the strings after every true boolean to be deleted. (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).
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. 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. 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. Is there a way to do this? If any clarification is needed, please let me know in the comments.
CodePudding user response:
This looks like a job for an Iterator:
boolean delete = false;
Iterator<?> iterator = (Iterator<?>) fruits.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
if (element instanceof Boolean b) {
delete = b;
} else if (delete) {
iterator.remove();
}
}
CodePudding user response:
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);
}
CodePudding user response:
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