I have an ArrayList of Strings.  I want to delete all elements with a particular value and the element immediately after that one.I have an ArrayList with the elements Ape, Bear, Cat, Dog, Emu, Fox, and Gopher.  Any time I see Cat, I want to delete Cat and the element immediately following it.  So in this example, I want to remove Cat and Dog.  But I don't know if it is going to be Dog, Dingo, Duck, or some other animal.The following code works:import java.util.ArrayList;public class TestList {    public static void main(String[] args) {        ArrayList<String> arrayList = new ArrayList<>();        arrayList.add(Ape);        arrayList.add(Bear);        arrayList.add(Cat);        arrayList.add(Math.random() > 0.5 ? Dog : Duck);        arrayList.add(Emu);        arrayList.add(Math.random() > 0.5 ? Fox : Ferret);        arrayList.add(Gopher);        for (int i = 0; i < arrayList.size(); i++) {            if (arrayList.get(i).equals(Ape)) {                arrayList.set(i + 1, Beagle);  //Changes Bear to Beagle            } else if (arrayList.get(i).equals(Cat)) {                arrayList.remove(i);  //Removes Cat                arrayList.remove(i);  //Removes the String following Cat            } else if (arrayList.get(i).equals(Emu)) {                arrayList.remove(i);  //Removes Emu                arrayList.remove(i);  //Removes the String following Emu            }        }    }}I know the order of the list will never change unless I am the one to change it, and I know that I will never search for the last element (Gopher in this case) or an element which immediately follows an element I have searched for (Dog/Duck or Fox/Ferret in this case).This code just feels dangerous to me, but I don't know why.  Is this code dangerous?  If so, why?
Removing an element and the element following from an ArrayList
java;concurrency
This code just feels dangerous to me, but I don't know why. Is this code dangerous? If so, why?If the method modifying the list is called from multiple threads,that's obviously dangerous:without synchronization,one thread might not see changes done by other threads,and you have no control over the order in which the different threads modify the list.In addition, the program depends on some pre-conditions:A next element must exist after the special ones used by your conditions. Otherwise you will get IndexOutOfBoundsExceptionElements after the special ones are deleted or modified, for no apparent logical reasonYour code looks hypothetical, not something realistic.If your real code is organized in a way that the preconditions make perfect sense,then your operations are not necessarily dangerous.If your code is not organized in a way that these operations are obviously correct, for example the class and method names involved don't make these operations obvious, then this can be dangerous. When code does something that is not obvious from its public interface, then what happens inside can be unexpected to users, lead to bugs, and be prone to errors.In addition, it's better to refer to types by interfaces instead of implementations. And, instead of adding elements to an ArrayList one by one, it can be ergonomic to use Arrays.asList, for example:List<String> list = new ArrayList<>(Arrays.asList(Ape, Bear, Dog));