'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:
After the quick-fix is applied:
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.
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: | |
Can be installed with plugin: | Java, 242.22892 |