Question

So I am attempting to create a LinkedHashedDictionary's Iterator member for a homework assignment, however I am having multiple issues regarding its types.

Iterator Interface:

package nhUtilities.containers2;

public interface Iterator<Element> extends Cloneable, java.util.Iterator<Element>
{
  public void reset ();
  public void advance ();
  public boolean done ();
  public Element get ();
  public boolean equals (Object obj);
  public boolean traverses (Object container);
  public Object clone ();
  public void setEqualTo (Iterator<Element> other);
  public boolean hasNext ();
  public Element next ();
  public void remove ();
}

In my code, I have a private class called EntryIterator. It extends an AbstractIterator, but implements the Iterator above.

My current implementation is as follows:

private class EntryIterator<Element> extends AbstractIterator<Element>
    implements Iterator<Element>
    {

        protected Iterator<Key> keyIterator;
        protected Dictionary<Key,Value> dictionary;

        public EntryIterator(Dictionary<Key,Value> dictionary)
        {
            keyIterator = keys.iterator();
            this.dictionary = dictionary;
        }

        public void reset()
        { keyIterator = keys.iterator(); }

        /**
         * @Requrie !this.done()
         */
        public void advance()
        { keyIterator.advance(); }

        public boolean done()
        { return keyIterator.done(); }

        // public Entry<Key,Value> get()
        // Violates initial Interface: Results in compile error.
        // Return type must be "Element"
        public Element get()
        {
            Key key = keyIterator.get();
            Value value = dictionary.get(keyIterator.get());
            return (Element) new Entry<Key,Value>(key, value);
        }

        public boolean traverses(Object container)
        {
            // TODO Iterator traverses
            return false;
        }

        public void setEqualTo(Iterator<Element> other)
        {
            this.keyIterator = ((EntryIterator<Element>) other).keyIterator;
            this.dictionary = ((EntryIterator<Element>) other).dictionary;
        }

    }

I have done multiple varieties of this class regarding its types, but none of them seem to be compatible with my Dictionary. Should I keep the formatting as is above, I get an error on my Dictionary's iterator() function:

public Iterator<Entry<Key,Value>> iterator()
{
    return new EntryIterator<Entry<Key,Value>>(this);
}

The error states it is "The return type is incompatible for Dictionary.iterator()"

Should I change the type of the EntryIterator class' type to:

private class EntryIterator<eEntry<Key,Value>> extends AbstractIterator<Element>
implements Iterator<Element>

I simply get an error saying "Syntax error expected on token '<'" as well as another incompatibility error on my Dictionary.Iterator() function.

Can someone point me in the right direction as to how I can link up all of these different types to get them to return what my contract for Dictionary demands?

I have attempted asking my question during the class, via email to the instructor, as well as one on one merely to be avoided. Any help would be much appreciated.

Was it helpful?

Solution

So, the problem was actually with my class instantiation... Here for everyone with similar issues:

 private class EntryIterator extends AbstractIterator<Entry<Key,Value>> implements 
nhUtilities.containers2.Iterator<Entry<Key, Value>>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top