Take the 2-minute tour ×

What is the best way to loop through an array when you need the index?

Option 1:

int len = array.length;
    array[i] = foo(i);

Option 2:

    array[i] = foo(i);

Or, does it not matter? Or is there a better way to do it? Just to point out the differences: In one case, the length of the array is evaluated as part of the test in the loop, although the compiler should normally optimize that.

Secondly, is ++i any different here from i++? I definitely prefer ++i if it is C++ but am not sure for Java.

share|improve this question
These shouldn’t be any different. –  user1675187 Sep 29 '12 at 2:41

3 Answers 3

up vote 5 down vote accepted

i++ vs ++i does not matter in this particular case. While C masters will tell you to store array.length in a variable, modern optimizing compilers make that unnecessary in this case as long as the length does not change in the loop. If you're really concerned you can benchmark both, but since .length doesn't actually need to traverse the entire array each time you'll be OK.

share|improve this answer
Great answer! Actually it's also recommended to always use option 1, isn't it? –  Roy Ling Sep 29 '12 at 3:10
Option 1 to which question? To i++ vs ++i? I prefer i++ for readability reasons, but I can see why ++i might be a better default, so that the new value of i is used in case you happen to change your code and don't notice the postincrement. ++i is also faster for stupid compilers which don't realize that the user doesn't care what value the expression has. –  Dan Sep 29 '12 at 3:15
No, I mean option 1 for .length. For ++i vs. i++, I think it's only a code style issue. –  Roy Ling Sep 29 '12 at 7:59
If all compilers will do the optimization, it's only a stylistic question anyway. I still feel like option 1 shows that the coder cares about performance, but I guess one could also argue that it shows that you don't realize that the Java compiler takes care of the optimization for you. –  Marshall Farrier Sep 29 '12 at 22:10

Generally those two methods are equivalent. You should note that in

for (int i = 0 ; i < foo() ; i++) {

the foo() is called once before each iteration (as opposed to only once before the first iteration), so you might want to take this into account for more complicated situations by perhaps doing something like

int n = foo();

which is analogous to your Option 1. So I would say Option 1 is certainly the safer of the two, but most of the time it should not make a significant difference which you use.

As for your second question: ++i first increments your variable and then retrieves it's value, i++ first retrieves the value and then increments. Just try these two pieces of code:

int i = 0;
int i = 0;

The first prints 1 but the second prints 0. Of course when ++i and i++ are alone it makes no difference.

share|improve this answer
I don't see i < foo() in the question. Has it been updated? :) –  Bhesh Gurung Sep 29 '12 at 3:26
@BheshGurung That foo function demonstrates how the rhs of for-loop conditions are evaluated repetitively, as I mentioned. When you have something like array.length on the rhs, it will make no difference. But when you have something like foo, you should exercise caution to some degree. –  arshajii Sep 29 '12 at 3:32

for whether to use "array.length" in for loop: Generally the compiler will do some optimization, as a result it is equivalent to using a variable in the for loop

for "i++" and "++i" In C++, ++i is preferred and more efficient, but in Java, they are equivalent in this case.

share|improve this answer

Your Answer


