Does calling anIterable.iterator() return a new or an existing Iterator? Does it depend on the Iterable implementation?
More specifically, would the following code work as expected (i.e. the inner loop would iterate from the beginning)?
for (Item itemA : items) {
for (Item itemB : items) {
....
}
}
If not, than how can this be achieved on an Iterable?
Iterable
Iterableis theInterface, we need to provide implementation for iterator() method.Implementation skeleton of the
MyIteratorclass:Using
MyCollectiongenerified, and with the new for-loopFor your 2nd question, output speaks, the inner loop will loop from the starting everytime.
Refereed this for examples