Possible duplicate:
to optimize the cycle
In java, I have a code block:
List e = {element1, element2, ...., elementn};
for(int i = 0; i < e.size(); i++){
};
and one more block:
List e = {element1, element2, ...., elementn};
int listSize = e.size();
for(int i = 0; i < listSize; i++){
};
I think the second block is better, because in the first block, if i++, we need to calculate e.size()again to compare the condition in the loop for. Right or wrong? And comparing the two blocks above, what is the best way to write? And why? Explain clearly and try this cycle yourself.
source
share