import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;

/** Borrar durante un recorrido: mal con for-each, bien con el iterador. */
public class RemoveDemo {

    public static void main(String[] args) {
        List<Song> songs = Songs.sample();
        try {
            for (Song s : songs) {
                if (s.seconds() < 200) {
                    songs.remove(s);                // modifica la lista por fuera del iterador
                }
            }
        } catch (ConcurrentModificationException e) {
            IO.println("for-each + remove: " + e.getClass().getName());
        }

        songs = Songs.sample();
        Iterator<Song> it = songs.iterator();
        while (it.hasNext()) {
            Song s = it.next();
            if (s.seconds() < 200) {
                it.remove();                        // borra el último elemento que devolvió next()
            }
        }
        IO.println("Quedan " + songs.size() + ":");
        for (Song s : songs) {
            IO.println("  " + s.seconds() + "  " + s.title());
        }
    }
}
