import java.util.* ;
import java.lang.* ;
class Ideone {
ArrayList< Integer> x = new ArrayList< Integer> ( ) ;
x.add ( 1 ) ; x.add ( 2 ) ; x.add ( 3 ) ; x.add ( 4 ) ; x.add ( 5 ) ;
for ( int i = 0 ; i < x.size ( ) ; i++ ) {
if ( i % 2 == 0 ) { //retira os elementos pares
x.remove ( i) ;
}
System .
out .
println ( "Tamanho atual = " + x.
size ( ) + ", i = " + i
) ; //só para ajudar visualizar }
for ( int i = 0 ; i < x.size ( ) ; i++ ) {
}
System .
out .
println ( "---------------------" ) ; x = new ArrayList< Integer> ( ) ;
x.add ( 1 ) ; x.add ( 2 ) ; x.add ( 3 ) ; x.add ( 4 ) ; x.add ( 5 ) ;
int tamanho = x.size ( ) ;
for ( int i = 0 ; i < tamanho; i++ ) {
if ( i % 2 == 0 ) {
x.remove ( i) ;
}
System .
out .
println ( "Tamanho atual = " + x.
size ( ) + ", i = " + i
) ; //só para ajudar visualizar }
for ( int i = 0 ; i < x.size ( ) ; i++ ) {
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CgpjbGFzcyBJZGVvbmUgewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uIHsKCQlBcnJheUxpc3Q8SW50ZWdlcj4geCA9IG5ldyBBcnJheUxpc3Q8SW50ZWdlcj4oKTsKCQl4LmFkZCgxKTsgeC5hZGQoMik7IHguYWRkKDMpOyB4LmFkZCg0KTsgeC5hZGQoNSk7CgkJZm9yIChpbnQgaSA9IDA7IGkgPCB4LnNpemUoKTsgaSsrKSB7CgkJCWlmIChpICUgMiA9PSAwKSB7IC8vcmV0aXJhIG9zIGVsZW1lbnRvcyBwYXJlcwoJCQkJeC5yZW1vdmUoaSk7CgkJCX0KCQkJU3lzdGVtLm91dC5wcmludGxuKCJUYW1hbmhvIGF0dWFsID0gIiArIHguc2l6ZSgpICsgIiwgaSA9ICIgKyBpKTsgLy9zw7MgcGFyYSBhanVkYXIgdmlzdWFsaXphcgoJCX0KCQlTeXN0ZW0ub3V0LnByaW50bG4oIi0tLSIpOwoJCWZvciAoaW50IGkgPSAwOyBpIDwgeC5zaXplKCk7IGkrKykgewoJCQlTeXN0ZW0ub3V0LnByaW50bG4oeC5nZXQoaSkpOwoJCX0KCQlTeXN0ZW0ub3V0LnByaW50bG4oIi0tLS0tLS0tLS0tLS0tLS0tLS0tLSIpOwoJCXggPSBuZXcgQXJyYXlMaXN0PEludGVnZXI+KCk7CgkJeC5hZGQoMSk7IHguYWRkKDIpOyB4LmFkZCgzKTsgeC5hZGQoNCk7IHguYWRkKDUpOwoJCWludCB0YW1hbmhvID0geC5zaXplKCk7CgkJZm9yIChpbnQgaSA9IDA7IGkgPCB0YW1hbmhvOyBpKyspIHsKCQkJaWYgKGkgJSAyID09IDApIHsKCQkJCXgucmVtb3ZlKGkpOwoJCQl9CgkJCVN5c3RlbS5vdXQucHJpbnRsbigiVGFtYW5obyBhdHVhbCA9ICIgKyB4LnNpemUoKSArICIsIGkgPSAiICsgaSk7IC8vc8OzIHBhcmEgYWp1ZGFyIHZpc3VhbGl6YXIKCQl9CgkJU3lzdGVtLm91dC5wcmludGxuKCItLS0iKTsKCQlmb3IgKGludCBpID0gMDsgaSA8IHguc2l6ZSgpOyBpKyspIHsKCQkJU3lzdGVtLm91dC5wcmludGxuKHguZ2V0KGkpKTsKCQl9Cgl9Cn0=
stdout
Tamanho atual = 4, i = 0
Tamanho atual = 4, i = 1
Tamanho atual = 3, i = 2
---
2
3
5
---------------------
Tamanho atual = 4, i = 0
Tamanho atual = 4, i = 1
Tamanho atual = 3, i = 2
Tamanho atual = 3, i = 3
stderr
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.remove(ArrayList.java:492)
at Ideone.main(Main.java:24)