You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Detects when a for loop should be used instead of a for-each loop.
It might make sense to only lint loops that do not use the loop variable?
Lint Name
FOR_EACH_SHOULD_BE_FOR_LOOP
Category
general
Advantage
No response
Drawbacks
No response
Example
inti = 0;
for (Stringelement : list) {
// element can be used in any way (does not matter)System.out.println(element);
System.out.println(i);
// i should be read at least once and only updated by 1 at the end of the loopi += 1; // or i++ or ++i
}
// TOOD: can it be converted if there are breaks in the for-each?
Could be written as:
// NOTE: if i is used after the for-each loop, one should declare it before the loop:// int i = 0;// for (; i < list.size(); i++) { ... }for (inti = 0; i < list.size(); i++) {
Stringelement = list.get(i);
System.out.println(element);
System.out.println(i);
}
The text was updated successfully, but these errors were encountered:
What it does
Detects when a for loop should be used instead of a for-each loop.
It might make sense to only lint loops that do not use the loop variable?
Lint Name
FOR_EACH_SHOULD_BE_FOR_LOOP
Category
general
Advantage
No response
Drawbacks
No response
Example
Could be written as:
The text was updated successfully, but these errors were encountered: