Inspectopedia 2025.2 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).

Example:

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

After the quick-fix is applied:

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

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

ForLoopReplaceableByForEach
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Java language level migration aids | Java 5

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 value:

Selected
Do not report iterations over untyped collections

Default value:

Not selected

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection ForLoopReplaceableByForEach

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025