Java

Java

Made by DeepSource

For loop can be converted into a foreach loop JAVA-W1089

Anti-pattern
Minor

If a for loop can be converted to a foreach loop, consider doing so, as it is a more concise and readable syntax.

This issue is raised when the Java analyzer detects that all elements of a list/array are being iterated over, in sequence, and only one element of the iterable is accessed in one loop iteration.

Bad Practice

for (int i = 0; i < list.size(); i++) {
    SomeType value = list.get(i);

    // do whatever is required with value.
}

Recommended

Use the foreach syntax to iterate over the iterable instead.

for (SomeType value : list) {
    // Do the required operation.
}