How about building a Queue with the elements you want to iterate over; when you want to add elements, enqueue them at the end of the queue, and keep removing elements until the queue is empty. This is how a breadth-first search usually works.
More Related Contents:
- How can I override next() for jagged array java?
- Calling remove in foreach loop in Java [duplicate]
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- ConcurrentModificationException despite using synchronized
- How to convert an Iterator to a Stream?
- Why is Java’s Iterator not an Iterable?
- Performance of traditional for loop vs Iterator/foreach in Java
- What is the difference between iterator and iterable and how to use them?
- Why isn’t this code causing a ConcurrentModificationException? [duplicate]
- Can we write our own iterator in Java?
- Iterator vs for
- Difference between Iterator and Listiterator?
- What are fail-safe & fail-fast Iterators in Java
- How to get a reversed list view on a list in Java?
- How Iterator’s remove method actually remove an object
- Iterate twice on values (MapReduce)
- Convert Iterator to List
- How does the enhanced for statement work for arrays, and how to get an iterator for an array?
- Difference between Java Enumeration and Iterator
- How can I implement the Iterable interface?
- Is it possible to merge iterators in Java?
- Generator functions equivalent in Java
- Java: why can’t iterate over an iterator?
- What is the best way to get the count/length/size of an iterator?
- For-each vs Iterator. Which will be the better option
- Iterating over and deleting from Hashtable in Java
- It does not throw exception ConcurrentModificationException [duplicate]
- Iterate with for loop or while loop?
- Provide an iterator over the contents of two lists simultaneously?
- How to check if an integer is in a set of given ranges? [closed]