Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Question about Java programming: How do I implement the 'Iterable' interface?

So to provide some context:

I wrote a class that functions like an ArrayList of 'Object' objects using a singly-linked LinkedList. From this ArrayList class we're supposed to implement the Iterable interface but looking at the provided :

http://docs.oracle.com/javase/7/docs/api/java/nio/...

I'm confused on how I am supposed to implement this interface.

Any videos or written explanations would be appreciated.

2 Answers

Relevance
  • John
    Lv 7
    4 years ago

    try this...

    import java.util.*;

    public class Program {

    public static void main(String... args) {

    MyArrayList lst = new MyArrayList();

    lst.add("one");

    lst.add("two");

    Iterator<String> itr = lst.iterator();

    while(itr.hasNext()) {

    System.out.println(itr.next());

    }

    }

    }

    class MyArrayList implements Iterable<String> {

    private final List<String> lst = new ArrayList<String>();

    @Override

    public Iterator<String> iterator() {

    return lst.iterator();

    }

    public void add(String value) {

    this.lst.add(value);

    }

    }

  • 4 years ago

    It's a two-step process, and the front part (actually implementing Iterable) is easy.

    public class ObjectList implements Iterable<Object> {

    .... etc.

    .... public Iterator<Object> iterator() {

    .... .... return new ObjectIterator(this);

    .... }

    }

    All that Iterable says is that your class has a method named "iterator" that returns and "Iterator" object.

    The Iterator takes a little more work since it need to implement three methods. (Two in Java 8 or later if you don't plan to implement remove() for your Iterator.) The above example uses a separate class as the iterator. That could be a top-level class (doesn't need to be public, since users of ObjectList.iterator() only need to know that returned value is an object that implements Iterator. That could look like:

    class ObjectIterator implements Iterator<Object> {

    .... private ObjectList list;

    .... private int index;

    .... private boolean atEnd;

    .... public ObjectIterator(ObjectList theList) {

    .... .... list = theList;

    .... .... index = 0;

    .... .... atEnd = list.isEmpty();

    .... }

    .... public boolean hasNext() {

    .... .... [return false if atEnd is true, or if index is at or beyond the length of the list]

    .... .... return true; // if the above didn't return false

    .... }

    .... public Object next {

    .... .... if (!hasNext()) throw new NoSuchElementException();

    .... .... return list.get(index++);

    .... }

    }

    You can add a remove() method if you want to allow deletes while iterating through the list. In Java 7 and earlier, you need to implement remove() always, throwing a UnsupportedOperationException if the iterator does not support deletion.

    You need to fill in the details of the hasNext method. It's up to you whether to call hasNext() from next() or repeat the code to get the test done without a method call. For maximum performance, define that ObjectIterator class as a private static class inside your ObjectList class, and then it has direct access to the ObjectList implementation and can access the length and get the indexed values without method calls.

Still have questions? Get your answers by asking now.