Question:
not understanding what statement in for loop executes? for(String c : a) java?
Hoa
2014-03-05 19:29:32 UTC
Hi, my professor went over a program that consisted of the code(in java)

String[] a = {"mirror", "basketball", "unicycle"};

for(String c: a)
{
.... and so on
}

I am unsure of what the code for(String c: a) executes or does? Any help would be appreciated thanks in advance!
Three answers:
Jon T
2014-03-05 21:35:55 UTC
Here is a summary/shorter answer



In java there is a for loop that has 3 parts

for (int x=0; x
String c = a[x];

...

}

int x=0 is the initialization of an iterator typically

x
x++ is the condition to execute each time the loop runs, typically increments the iterator



The advanced for loop of for(String c: a) is a shortcut that does the normal for loop and extracting the variable for you. Only works when the code knows the object type inside the list (and the list cannot be null or else you get a NPE (NullPointerException). typically used with



List listOfObjects = getListOfObjects(); // gets some list containing MyObject



for (MyObject obj: listOfObjects) {

obj.doSomething();

}
Ratchetr
2014-03-05 20:03:21 UTC
This is a For Each loop.



In C#, you would write it as:

foreach( String c in a )

... Do something with c



I think the C# syntax is a bit more intuitive than Java, but they do the same thing:



The first time through the loop, the String variable c will be set to the first element in the array a. "mirror" in this case. The next time through the loop, c will be set to the second element in the array. basketball in this case. This continues until the end of the array is hit, which is when the loop will exit.



Even Oracle agrees that the C# syntax is more intuitive. See link where they say:

When you see the colon (:) read it as “in.” The loop above reads as “for each TimerTask t in c.”



Makes you wonder why they didn't just add foreach and in as keywords in Java, and let you write it the intuitive way.
Jamie
2014-03-05 20:12:24 UTC
The code that you are referring to is called a "for each" loop, and it is useful for iterating through arrays, as you don't have to include any of the conditions for the loop.



In the code that you posted in your question, an array of type String was declared and defined to have three values inside of it - "mirror", "basketball", and "unicycle". These values would be stored in three consecutive indices of the "a" array, starting at index 0.



Without using a "for each" loop, if one wants to go through the contents of an array, they could use a standard "for loop", where they would have to include conditions for the loop, telling it where to start, where to end, and what to do after each iteration (add one, etc.).



Here's an example a regular "for loop" that would go through (or iterate through) the "a" array that you posted in your question.



for(int i = 0; i < a.length; i++){

//...and so on

}



In the above "for loop", the loop would start iterating at the first index of the "a" array (index 0), and would then continue to iterate through the loop until it got to one less than the value of the number of indices in the "a" array. (This is to avoid an arrayIndexOutOfBounds exception, as the length of the array is 3, but since the array has indices 0-2, the loop would iterate out of the boundaries of the array.) After that, the value of "i" would be increased by one, up until the loop went through a.length (3) iterations.



Instead of writing a regular "for loop" to go through an array, one can write a "for each" loop, which essentially does the same thing as the "for loop" above, but without having to add the conditions of the loop. The "for each" loop will simply stop once it has iterated, or gone through, every index of the array.



The "for each" loop that you included in your question will create a new String, called "c", and will store whatever value is in the current index of the "a" array in the "c" variable. You can then do whatever you like with that variable or data within the body of the loop.



Each time the loop iterates, the value of "c" will change to be the value of the next index of the "a" array, up until the loop reaches the last index, after which it will stop executing.



Best of luck and I hope I helped you!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...