Files
2025-02-05 04:43:28 +00:00

31 lines
1.5 KiB
HTML

<html>
<body>
Reports <code>for</code> loops that iterate over collections or arrays,
and can be automatically replaced with an enhanced <code>for</code> loop (foreach iteration syntax).
<!-- 'foreach' still used in this description so that the inspection continues to be found using this keyword -->
<p><b>Example:</b></p>
<pre><code>
for (Iterator&lt;String&gt; iterator = list.iterator(); iterator.hasNext(); ) {
String item = iterator.next();
System.out.println(item);
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
for (String item : list) {
System.out.println(item);
}
</code></pre>
<!-- tooltip end -->
<p>
Use the <b>Report indexed 'java.util.List' loops</b> option to find loops involving <code>list.get(index)</code> calls.
Generally, these loops can be replaced with enhanced <code>for</code> loops,
unless they modify an underlying list in the process, for example, by calling <code>list.remove(index)</code>.
If the latter is the case, the enhanced <code>for</code> loop may throw <code>ConcurrentModificationException</code>.
Also, in some cases, <code>list.get(index)</code> loops may work a little bit faster.</p>
<p>
Use the <b>Do not report iterations over untyped collections</b> option to ignore collections without type parameters.
This prevents the creation of enhanced <code>for</code> loop variables of the <code>java.lang.Object</code> type and the insertion of casts
where the loop variable is used.</p>
</body>
</html>