Example code:
int a[] = new int[]{0, 1, 2, 3}; int result = 0; for (int i : a) result += i;
Is the loop guaranteed to iterate across a[0], a[1], a[2], a[3] in that order? I strongly believe the answer is yes, but this page seems to not unambiguously state order.
Got a solid reference?
According to the JLS, The enhanced
forstatement, your for-loop is equivalent to‘where
arrayandindexare compiler-generated identifiers that are distinct from any other identifiers (compiler-generated or otherwise) that are in scope at the point where the enhancedforstatement occurs.’ (slightly paraphrasing the variable names here).So yes: the order is absolutely guaranteed.