17 lines
685 B
Plaintext
17 lines
685 B
Plaintext
![]() |
Generally it's not recommended to modify a collection while it's iterated and most JDK collection implementations don't support it. This may sometimes lead to a ConcurrentModificationException and it could be the source of incorrect or unspecified behaviors in the code.
|
||
|
|
||
|
This rule raises an issue when a method modifies the size of a collection, while the same collection is iterated.
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
[source,java]
|
||
|
----
|
||
|
public static void foo(List<String> lst) {
|
||
|
for (String element : lst) {
|
||
|
if (element.startsWith("x")) {
|
||
|
lst.remove(element); // Noncompliant: lst size has been modified by "remove" call while it's iterated.
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
----
|