0

I'm unsure how to add Iterator code to my DLL class

i tried two approaches but i get compile errors in both cases:

public static void main(String[] args)
{
    DLL myList = new DLL() ;

...

public java.util.Iterator iterator()
(
    return new LRIterator() ;
)

private class LRIterator implements Iterator
{
 ...
}
... 

compile error: ';' expected public java.util.Iterator iterator();

other approach:

public static void main(String[] args)
{
    DLL myList = new DLL() ;
    ...
    Iterator itr = myList.iterator(); 
    while(itr.hasNext()) {

    Object element = itr.next(); 
    System.out.print(element + " ");
}
...

copile error: cannot find symbol Iterator = myList.iterator();

1 Answer 1

5

You may want to change ( and ) into { and } in the following snippet:

public java.util.Iterator iterator()
(
    return new LRIterator() ;
)

A few extra pointers.

  • I suggest you don't use the raw type of Iterator and instead use Iterator<T> if your list contains elements of type T.

  • I suggest you let your class which has the iterator() method implement the Iterable<T> interface. This allows users of your class to use it in for each loops for instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Deuh! I'm sorry for asking the question now... Thanks for the additional tips! – getting a new error: LRIterator is not abstract and does not override abstract method remove()...any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.