Issue
Does Java's foreach loop start at the first object and in a linear fashion work it's way to the end? For example
String[] names = new String[] {"Zoe", "Bob", "Charlie", "Alex"};
for(String name : names) {
//do stuff...
}
Is the string "Zoe" always processed first, followed by "Bob" etc? No sorting happens? I've tested it out myself and haven't found any but I need a guarantee and couldn't find anything in the docs.
Solution
Yes. The order is not changed. This applies to all types of collections of the Java Collection Framework implementing the iterator interface that is used by the for-loop. If you want to sort your Array, you can use Arrays.sort(names)
Answered By - Simulant
Answer Checked By - David Goodson (JavaFixing Volunteer)