How Arrays work with Foreach, when they don't have Iterable implemented?

arrays Iterable

20th Apr 2017, 6:55 AM
harish swami
harish swami - avatar
2 Answers
+ 4
 int arr[]={1,2,3,4,5};        for(int i:arr){        System.out.println(i);    } It just stores all elements one by one replacing old elements for every new iteration of array or a collection in the declared variable,like in abv example it stores 1st element of array in "i" and then u do whatever u want with that data,and it keeps going till the last element. How it works you say..it might have an iterative method or sortof methods internally,which gets invoked when you use foreach loop..Not sure though :0
20th Apr 2017, 7:38 AM
Manideep - avatar
for each loop automatically incremented. You don't need to know the size of an array. this is the magic of for each loop.
20th Apr 2017, 11:11 AM
Sumit Kale
Sumit Kale - avatar