3

I don't understand how a for each loop can iterate through an Array in Java. My understanding is that for each loops can iterate though any class that implements the Iterable interface, but Arrays in Java don't implement Iterable, so how is it possible a for each loop can be used on them?

timg
  • 381
  • 2
  • 13

2 Answers2

4

If the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. That's why it can be used to loop through arrays. See the Java Language Specification for further details.

Part of this answer was exempted from here. You can take a look at that question too.

I would like to add, if you want you can easily convert java array to Iterable:

Integer arr[] = { 1, 2, 3, 4, 5};

List<Integer> list = Arrays.asList(arr);
// or
Iterable<Integer> iterable = Arrays.asList(arr);
Plabon Dutta
  • 6,819
  • 3
  • 29
  • 33
  • Is there any source that describes WHY they decided the way they did? In particular why they decided that Array will not implement Iterable? I can think about some reasons, but I am curious … – tquadrat Jun 16 '20 at 05:56
  • You just copied & pasted someone's answer as your own. Instead, mark as duplicate. You didn't even try to reword it.. -1 – Vince Jun 16 '20 at 06:07
  • Thanks for your words @VinceEmigh. I didn't mark it as duplicate because the questions are different. I just took the part which would make sense for him. Also, I've mentioned the actual link. I don't think I've done anything against community policy. #Peace – Plabon Dutta Jun 16 '20 at 06:48
  • @tquadrat Hera are some great answers that might enlighten you a bit. https://stackoverflow.com/questions/1160081/why-is-an-array-not-assignable-to-iterable – Plabon Dutta Jun 16 '20 at 06:51
  • Do you know if there are any data structures outside of array that can be used in for each loop without iterable implemented? – timg Jun 17 '20 at 03:52
  • Not anything that I can think of at the moment. Thing is, most of the well-known data structures in Java (except `Map`) extends the `Collection` interface, and the `Collection` interface extends the `Iterable` interface. So, there's not much option for a data structure outside of an array to be used in `for-each` loop. – Plabon Dutta Jun 17 '20 at 12:04
2

As per JLS:

The enhanced for statement has the form:

EnhancedForStatement: for ( {VariableModifier} UnannType VariableDeclaratorId : Expression ) Statement

EnhancedForStatementNoShortIf: for ( {VariableModifier} UnannType VariableDeclaratorId : Expression ) StatementNoShortIf

Java foreach loop or enhanced for statement is translated into a basic for statement, as follows:

  1. If the type of Expression is a subtype of Iterable<X> for some type argument X, then let I be the type java.util.Iterator<X>; otherwise, let I be the raw type java.util.Iterator.

The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {
    {VariableModifier} TargetType Identifier =
        (TargetType) #i.next();
    Statement
}
  1. Otherwise, the Expression necessarily has an array type, T[].

The enhanced for statement is equivalent to a basic for statement of the form:

T[] #a = Expression;
L1: L2: ... Lm:
for (int #i = 0; #i < #a.length; #i++) {
    {VariableModifier} TargetType Identifier = #a[#i];
    Statement
}
Community
  • 1
  • 1
Maverick
  • 1,519
  • 18
  • 32