Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create an iterator for the inside of a nested loop in Java?
    primarykey
    data
    text
    <p>I can write a nested loop to iterate over the elements of a nested array, the for-each elegantly hides the details of the traversal over each level of the nested array:</p> <pre><code>Foo[][] dbl_array; public void do_all() { // Iterate over both levels of a nested array, invoking "bar" on each inner element. for (final Foo[] arr_1d : dbl_array) { for (final Foo el : arr_1d) { el.bar(); } } } </code></pre> <p>But the problems with this approach are:</p> <ol> <li>The fact that a doubly-nested loop is needed to traverse the data structure is painfully obvious here.</li> <li>I have to copy this nested loop for every function I need to call on the inner elements.</li> <li>This breaks the encapsulation of the method for traversing the structure. I may choose to implement the nested array with some other structure and don't want to change every copy of the nested iterations to whatever traversal method is needed.</li> <li>The structure of the nested for-each loops is inside out from what is needed. Instead of having the desired function call inside the nests, an Iterator should handle the data structure traversal internally, exposing each entry encountered during the traversal.</li> </ol> <p>So...how do I change this so that I implement an Iterator that I could invoke like:</p> <pre><code>Foo_Iterator fi = Foo.iterator(); for (final Foo el : fi) { // The Iterator hides the traversal details from the caller. el.bar(); // The desired function is invoked on each element encountered. } </code></pre> <p>This would leave the details of how the iteration is done to the Foo_Iterator class.</p> <p>My question is "How do I write Foo_Iterator, keeping track of the state of the nested iterators? I think it would look something like the following, but I'm missing the bits that keep track of the state.</p> <pre><code>class Foo_Iterator extends Whiz implements Iterator { public Foo_Iterator() { // Initialize state based on access to the superclass Whiz. } public boolean hasNext() { // Is there an elegant way to save the state of both iterators between each call to hasNext() and next()? // The "inelegant" way would be to keep track of the inner and out array indices, // comparing the current index to the array length... } public Foo next() { // Access the "next" in the nested sequence. } public void remove() { // I probably won't implement or need/use this one. } } </code></pre> <p>Any suggestions on how to do this the "elegant" way?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload