- Executores
-
Aprender
Aprender Python Exemplos executáveis, NumPy, pandas e mais Aprender IA / Machine LearningRedes neurais, ML e LLMs do zero Aprender C e C++ Ponteiros, memória, STL e C++ moderno Aprender C#Fundamentos, POO, LINQ, async e exercícios .NET Aprender MatplotlibGráficos de linha, barras, dispersão e pizza com visualizadores Aprender RustOwnership, traits, tratamento de erros e threads Aprender GoSlices, interfaces, goroutines e channels Algoritmos em Python Busca, ordenação, recursão e visualizações O(n) Algoritmos em C e C++ Compare implementações em C e C++ Programação para crianças Aprenda a programar brincando!
- QuizzesNovo
- DesafiosNovo
- Para professoresNovo
- Atualizações
- Me pague um café
Quiz de Java: Iterators & ConcurrentModificationException
This hard Java quiz is about changing a collection while you iterate over it. You will see when the for-each loop throws ConcurrentModificationException (and the odd case where it does not), and practice the safe ways: Iterator.remove(), removeIf(), ListIterator and CopyOnWriteArrayList.
Pergunta de exemplo
Pergunta 1 de 10What happens when you run this?
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4));
for (Integer n : list) {
if (n == 2) list.remove(n);
}
System.out.println(list);
}
}
Mostrar a resposta
A. ConcurrentModificationException The for-each loop uses a hidden iterator. Removing through the list itself changes its modification count, and the iterator's next next() call notices and throws ConcurrentModificationException.
O que este quiz cobre
- Why removing inside a for-each loop throws
Iterator.remove()and its rulesremoveIf()as the simple fix- Index loops that skip elements
ListIterator.add()andCopyOnWriteArrayList
Perguntas frequentes
Preciso de uma conta?
Não. Qualquer pessoa pode jogar. Entre para guardar seu XP, sequência, badges e histórico.
Posso refazer?
Pode. Sua melhor nota fica salva, e as perguntas e respostas são embaralhadas a cada vez.