Q:

ConcurrentModificationException when deleting an element from ArrayList

Java is throwing ConcurrentModificationException when I am running the following code. Any idea why is that?
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Hello");
list1.add("World");
list1.add("Good Evening");

for (String s : list1){
        list1.remove(2);
    System.out.println(s);
}

A:

If you take a look at documentation of ConcurrentModificationException you will find that 

This exception may be thrown by methods that have detected concurrent
  modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify
  a Collection while another thread is iterating over it
...
Note that this exception does not always indicate that an object has
  been concurrently modified by a different thread. If a single thread
  issues a sequence of method invocations that violates the contract of
  an object, the object may throw this exception. For example, if a
  thread modifies a collection directly while it is iterating over the
  collection with a fail-fast iterator, the iterator will throw this
  exception.

Important thing about this exception is that we can't guarantee it will always be thrown as stated in documentation 

Note that fail-fast behavior cannot be guaranteed as it is, generally
  speaking, impossible to make any hard guarantees in the presence of
  unsynchronized concurrent modification. Fail-fast operations throw
  ConcurrentModificationException on a best-effort basis.

Also from ArrayList documentation

The iterators returned by this class's iterator and listIterator
  methods are fail-fast: if the list is structurally modified at any
  time after the iterator is created, in any way except through the
  iterator's own remove or add methods, the iterator will throw a
  ConcurrentModificationException.

(emphasis mine)
So you can't manipulate content of Collection (in your case List) while iterating over it via enhanced for loop because you are not doing it via iterator for-each is using internally. 
To solve it just get your own Iterator and use it in your loop. To remove elements from collection use remove like in this example 
Iterator<String> it = list1.iterator();
int i=0;
while(it.hasNext()){
    String s = it.next();
    i++;
    if (i==2){
        it.remove();
        System.out.println("removed: "+ s);
    }
}

