mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
32 lines
1.1 KiB
HTML
32 lines
1.1 KiB
HTML
<html>
|
|
<body>
|
|
Reports <code>list.remove(index)</code> calls inside an ascending counted loop.
|
|
<p>
|
|
This is suspicious as the list becomes
|
|
shorter after the removal, and the next element gets skipped. A simple fix is to decrease the index variable
|
|
after the removal,
|
|
but probably removing via an iterator or using the <code>removeIf()</code> method (Java 8 and later) is a more robust alternative.
|
|
If you don't expect that <code>remove()</code> will be called more than once in a loop, consider adding a <code>break</code> after it.
|
|
</p>
|
|
|
|
<p><b>Example:</b></p>
|
|
<pre><code> public static void main(String[] args) {
|
|
process(new ArrayList<>(
|
|
Arrays.asList("1", "2", "|", "3", "4")));
|
|
}
|
|
|
|
static void process(List<String> list) {
|
|
for (int i = 0; i < list.size(); i++) {
|
|
if (list.get(i).equals("|")) {
|
|
list.remove(i);
|
|
continue;
|
|
}
|
|
System.out.println(list.get(i));
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>The code looks like <code>1 2 3 4</code> is going to be printed, but in reality, <code>3</code> will be skipped in the output.</p>
|
|
<!-- tooltip end -->
|
|
<p><small>New in 2018.2</small></p>
|
|
</body>
|
|
</html> |