Files
openide/java/java-impl/resources/inspectionDescriptions/ListRemoveInLoop.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

29 lines
953 B
HTML

<html>
<body>
Reports <code>List.remove(index)</code> called in a loop that can be replaced with <code>List.subList().clear()</code>.
<p>The replacement
is more efficient for most <code>List</code> implementations when many elements are deleted.</p>
<p>Example:</p>
<pre><code>
void removeRange(List&lt;String&gt; list, int from, int to) {
for (int i = from; i &lt; to; i++) {
list.remove(from);
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
void removeRange(List&lt;String&gt; list, int from, int to) {
if (to &gt; from) {
list.subList(from, to).clear();
}
}
</code></pre>
<p>
The quick-fix adds a range check automatically to prevent a possible <code>IndexOutOfBoundsException</code> when the minimal value is bigger
than the maximal value. It can be removed if such a situation is impossible in your code.
</p>
<!-- tooltip end -->
<p><small>New in 2018.2</small></p>
</body>
</html>