简体   繁体   中英

Removing specific items in Arraylist

I am struggling with removing items from my arraylist. More specifically, I have two arraylists, being persons and vulcanos. Both are located in a grid. Each time step, people can move or not (this happens randomly, to the adjecant cells), while volcanos are static during the whole simulation. Volcanos can erupt or not. This is determined by a probability defined by the user. When at a certain timestep a person is located at the same cell as an erupting volcano, this person dies. But I don't know how I can remove that specific person from my array. So far I got the following (is repeated for each time step).

for (int j = 0; j < persons.size(); j++) {
    for (int k = 0; k < volcanos.size(); k++) {
        if ((persons.get(j).getXCoordPerson()) == (volcanos.get(k).getXCoordVolcano())
                && (persons.get(j).getYCoordPerson()) == (volcanos.get(k).getYCoordVolcano())
                && (volcanos.get(k).getEruptionStatus() == true)) {
            // all persons being on the same cell as an erupting volcano die
        }
    }
}

Try to mark your people as dead or harmed(etc.) or add them to another list. Then delete them after loop like

persons.removeAll(deadPeople);

If you don't mind working with Iterable instead of List , and if you don't mind taking dependency on Guava, then why not use Iterables ?

Iterable<Person> filtered = Iterables.filter(persons, new Predicate<Person>() {
            @Override
            public boolean apply(Person person) {
                for (Volcano v : volcanos) {
                    if (v.getEruptionStatus() && 
                        v.getXCoordVolcano() == person.getXCoordPerson() && 
                        v.getYCoordVolcano() == person.getYCoordPerson()) {
                        return false;
                    }
                }
                return true;
            }
        });

This will filter out all dead Persons , and will leave filtered with only living people

You can do this with an Iterator too:

for(Iterator<Person> personIterator = persons.iterator(); i.hasNext(); ) {
    Person person = personIterator.next();
    for(Volcano volcano : volcanos) {
        if(volcano.getEruptionStatus() && 
           volcano.getYCoordVolcano() == person.getYCoordPerson() &&
           volcano.getXCoordVolcano() == person.getXCoordPerson()) {
               personIterator.remove();
        }
    }
}

In this case, you are using the iterator to do the removing, leaving the ArrayList in the state you want as a side effect of the loops.

One thing to watch out for is that you do not modify the list of people during this loop or you will end up with a ConcurrentModificationException . I'm assuming you have one single thread operating, and that this is for schoolwork or something similar. If you were doing this in the real-world, you would have to account for the fact that both lists (people and volcanos) could be changing at any time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM