List<String> objList = getData();
for (int i = 0; i < objList.size(); i++) { // Noncompliant
// execute code
}
When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. The example provided below illustrates what should be avoided.
List<String> objList = getData();
for (int i = 0; i < objList.size(); i++) { // Noncompliant
// execute code
}
List<String> objList = getData();
int size = objList.size();
for (int i = 0; i < size; i++) {
// execute code
}