Why does Android Studio want me to use for everyone, not for a loop?

This question fought me in time. Why sometimes Android Studio wants me to use For Every and not For Loop, because when I use For Loop, I get a warning that I can use for everyone (and with Alt + Enter it offers me autofix).

For example, suppose this code

String a = ""; String[] array = {"A","B","C"}; for(int i = 0; i < array.length; i++) { a += array[i]; a += ","; } 

I get a warning

and this fix offered by Android Studio

 for (String anArray : array) { a += anArray; a += ","; } 

Is he more perfect? Is there a reason why I should get a warning for actual use only for the loop?

Or when is it better for the cycle or for everyone?

+7
java foreach android-studio for-loop
source share
2 answers

From the book: Effective Java (2nd Edition) by Joshua Bloch

The for-each loop introduced in version 1.5 gets rid of clutter and the possibility of error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

  // The preferred idiom for iterating over collections and arrays for (Element e : elements) { doSomething(e); } 

When you see a colon (:), read it as "in". Thus, the above loop reads as "for each element e in the elements." Note that there is no usage penalty for each loop, even for arrays. In fact, it can give a slight performance advantage over the usual loop in some cases, since it only calculates the limit of the index array once. Although you can do it manually (paragraph 45), programmers do not always do this.


The following is a comparison of http://developer.android.com/training/articles/perf-tips.html#Loops :

 static class Foo { int mSplat; } Foo[] mArray = ... public void zero() { int sum = 0; for (int i = 0; i < mArray.length; ++i) { sum += mArray[i].mSplat; } } public void one() { int sum = 0; Foo[] localArray = mArray; int len = localArray.length; for (int i = 0; i < len; ++i) { sum += localArray[i].mSplat; } } public void two() { int sum = 0; for (Foo a : mArray) { sum += a.mSplat; } } 

zero() is the slowest since the JIT cannot yet optimize the cost by getting the length of the array once for each iteration through the loop.

one() faster. It outputs everything to local variables, avoiding searching. Only array length provides performance benefit.

two() is the fastest for devices without JIT and indistinguishable from one() for devices with JIT. It uses the extended loop syntax introduced in version 1.5 of the Java programming language.

So, you should use the extended extended for loop by default, but the handwritten counting loop for the critical value of the ArrayList iteration.

+24
source share

It is simply readable when you simply repeat without looking forward or backward. Well, this can be read if you choose a good variable name (as opposed to anArray for String : D). This is a bit safer because if you were a really stupid programmer, you could write array[2 * i] and throw an ArrayOutOfBoundsException . Other than that, I find it a little exaggerated to ask you to reorganize it ... this is a common idiom in Java.

+1
source share

All Articles