Java for-loop practice

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

Option 1:

int len = array.length; for (int i = 0; i < len; ++i) { array[i] = foo(i); } 

Option 2:

 for (int i = 0; i < array.length; i++) { array[i] = foo(i); } 

Or does it not matter? Or is there a better way to do this? Just 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 usually needs to optimize it.


Secondly, ++i any other here from i++ ? I definitely prefer ++i if it's C ++, but I'm not sure about Java.
+8
java arrays coding-style for-loop
source share
4 answers

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 this unnecessary in this case if the length does not change in the loop. If you are really worried, you can compare both, but since .length does not actually need to move the entire array every time you are fine.

+6
source share

Usually these two methods are equivalent. It should be noted that in

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

foo() is called once before each iteration (and not just once before the first iteration), so you can take this into account in more complex situations, perhaps by doing something like

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

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


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

 int i = 0; System.out.println(++i); ------------------------ int i = 0; System.out.println(i++); 

The first seal is 1 , and the second is 0 . Of course, when ++i and i++ are the same, it doesn't matter.

+6
source share

in order to use "array.length" in a for loop: As a rule, the compiler will perform some optimization, as a result, it is equivalent to using a variable in a for loop

for "i ++" and "++ i" In C ++ ++ I am preferable and more efficient, but in Java they are equivalent in this case.

0
source share

In addition to arshaji's answer, I wanted to know if there was a performance advantage when using size() in a loop and saving it in advance. I believe that the result shows that the compiler does optimization and access to the length of the list is the same as accessing the variable (I was worried that the fact that it had to go through the function would slow everything down).

Here is the time it takes for these two for different loop methods:

 for(long i = 0 ; i < mylist.size(); i++){} VS for(long i = 0 ; i < 10_000_000; i++){} 

Here is the result for a list of ten million items:

 fixed length: ,162,157,151,157,156,159,157,149,150,170,158,153,152,158,151,151,156,156,151,153 getSize: ,164,156,159,154,151,160,162,152,154,152,151,149,168,156,152,150,157,150,156,157 import java.util.ArrayList; import java.util.List; public class Main { final static int LENGTH_SAMPLE = 20; final static long LENGTH = 10_000_000; public static void main(String[] args) { List<Long> mylist = new ArrayList<>(); for(long i = 0 ; i < LENGTH; i++){ mylist.add(i); } System.out.println("fixed length:"); for(int i = 0 ; i < LENGTH_SAMPLE; i++){ System.out.printf("," + fixedSize(mylist)); } System.out.println(""); System.out.println("getSize:"); for(int i = 0 ; i < LENGTH_SAMPLE; i++){ System.out.printf("," + fctSize(mylist)); } } private static long fixedSize(List list){ long start = System.currentTimeMillis(); for(long i = 0 ; i < LENGTH; i++){ System.currentTimeMillis(); } return System.currentTimeMillis() - start; } private static long fctSize(List list){ long start = System.currentTimeMillis(); for(long i = 0 ; i < list.size(); i++){ System.currentTimeMillis(); } return System.currentTimeMillis() - start; } } 
0
source share

All Articles