Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this for-each loop work? [duplicate]

Tags:

java

foreach

In this code, why isn't my array initialised as I want it to? Is the for-each loop not designed to do that, or am I just not using it correctly?

    int[] array = new int[5];

    //initialise array -> Doesn't work! Array still full of 0's
    for(int i : array)
        i = 24;
like image 912
masher Avatar asked Apr 06 '09 03:04

masher


People also ask

Does for-each loop make a copy?

forEach() does not make a copy of the array before iterating.

What can for-each loop not do?

For-each cannot be used to initialize any array or Collection, because it loops over the current contents of the array or Collection, giving you each value one at a time. The variable in a for-each is not a proxy for an array or Collection reference.

When can for-each loop not be used?

Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel.

Will you use a for-each loop or just a simple for loop?

Only use the for-each loop when you want to loop through all the values in an array or list. If you only want to loop through part of an array or list use a for loop instead. Also use a for loop instead of a for-each loop if you want to change any of the values in the array or list.


1 Answers

The for-each loop will not work for this case. You cannot use a for-each loop to initialize an array. Your code:

int[] array = new int[5];
for (int i : array) {
    i = 24;
}

will translate to something like the following:

int[] array = new int[5];
for (int j = 0; j < array.length; j++) {
    int i = array[j];
    i = 24;
}

If this were an array of objects, it would still fail. Basically, for-each assigns each entry in the collection or array, in turn, to the variable you provide, which you can then work with. The variable is not equivalent to an array reference. It is just a variable.

For-each cannot be used to initialize any array or Collection, because it loops over the current contents of the array or Collection, giving you each value one at a time. The variable in a for-each is not a proxy for an array or Collection reference. The compiler does not replace your "i" (from "int i") with "array[index]".

If you have an array of Date, for example, and try this, the code:

Date[] array = new Date[5];
for (Date d : array) {
    d = new Date();
}

would be translated to something like this:

Date[] array = new Date[5];
for (int i = 0; i < array.length; i++) {
    Date d = array[i];
    d = new Date();
}

which as you can see will not initialize the array. You will end up with an array containing all nulls.

NOTE: I took the code above, compiled it into a .class file, and then used jad to decompile it. This process gives me the following code, generated by the Sun Java compiler (1.6) from the code above:

int array[] = new int[5];
int ai[];
int k = (ai = array).length;
for(int j = 0; j < k; j++)
{
    int i = ai[j];
    i = 5;
}
like image 123
Eddie Avatar answered Oct 26 '22 23:10

Eddie