S. Sharma
S. Sharma

Reputation: 213

What is a nested and parallel iterator in Java?

I was working on a programming assignment for an online course. I submitted my assignment and received a grade along with a log of the tests that they performed. In that log, I saw this:

Test 9: create two nested iterators over the same randomized queue

n = 10 - two inner iterators return the same sequence of items - they should return the same set of items but in a different order ...

I also saw a similar message with "nested" replaced with "parallel."

My code for the iterator of my class is as follows:

public Iterator<Item> iterator() { return new RandomizedQueueIterator(); }
  private class RandomizedQueueIterator implements Iterator<Item> {
    private int start = head;
    public boolean hasNext() { return start < tail; }
    public Item next() {
      if (start == tail)
         throw new NoSuchElementException();
      return a[start++];
    }
    public void remove() { throw new UnsupportedOperationException(); }
  }

where head is the pointer to the current element on top of my Queue data structure, and tail is the current element at the end of my Queue data structure.

The code provided by the course (Algorithms I by Sedgewick) only gave us information about how to do the above and nothing more.

Any help would be appreciated. Thanks.

Upvotes: 0

Views: 218

Answers (1)

ruakh
ruakh

Reputation: 183582

"Nested iterators" and "parallel iterators" are not standard terms, but I can at least explain this part:

n = 10 - two inner iterators return the same sequence of items - they should return the same set of items but in a different order ...

It's saying that your iterators gave the same ten items in the same order, when they should have given the same ten items in different orders.

To write this in bug-report form, it's saying you have this bug:

STEPS TO REPRODUCE: Create a RandomizedQueue with ten elements. Obtain two iterators (by calling iterator() twice). Iterate over each (by repeatedly calling hasNext() and next() until hasNext() returns false), and record the results.

EXPECTED BEHAVIOR: The two iterators should give the same ten elements, but in different (random) orders. For example, one iterator might give [1, 8, 3, 2, 6, 10, 9, 4, 7, 5] and the other might give [8, 6, 3, 2, 7, 9, 10, 1, 5, 4].

ACTUAL BEHAVIOR: The two iterators give the same elements in the same order. For example, they both give [1, 8, 3, 2, 6, 10, 9, 4, 7, 5].

Upvotes: 1

Related Questions