Inspectopedia Help

'for' loop can be replaced with enhanced for loop

Reports for loops that iterate over collections or arrays, and can be automatically replaced with an enhanced for loop (foreach iteration syntax).


for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) { String item =; System.out.println(item); }

After the quick-fix is applied:

for (String item : list) { System.out.println(item); }

Use the Report indexed 'java.util.List' loops option to find loops involving list.get(index) calls. Generally, these loops can be replaced with enhanced for loops, unless they modify an underlying list in the process, for example, by calling list.remove(index). If the latter is the case, the enhanced for loop may throw ConcurrentModificationException. Also, in some cases, list.get(index) loops may work a little bit faster.

Use the Do not report iterations over untyped collections option to ignore collections without type parameters. This prevents the creation of enhanced for loop variables of the java.lang.Object type and the insertion of casts where the loop variable is used.

This inspection depends on the Java feature 'For-each loops' which is available since Java 5.

Inspection options

Here you can find the description of settings available for the 'for' loop can be replaced with enhanced for loop inspection, and the reference of their default values.

Report indexed 'java.util.List' loops

Default: Selected

Do not report iterations over untyped collections

Not selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.16690

Last modified: 29 April 2024