...
Code Block | ||||
---|---|---|---|---|
| ||||
// ... for (final Integer i: list) { // ... |
Compliant Solution
This compliant solution processes the 'modified' list, but leaves the actual list unchanged.
Code Block | ||||
---|---|---|---|---|
| ||||
// ... for (final Integer i: list) { Integer item = i; if (first) { first = false; item = new Integer(99); } System.out.println(" New item: " + item); // process item } // ... |
Risk Assessment
Assignments to the loop variable of an enhanced for
loop (for-each idiom) fail to affect the overall iteration order, lead to programmer confusion, and can leave data in a fragile or inconsistent state.
...