6

I'm a bit confused about how to implement a custom iterator for a class in Java. I'm required to essentially make an ArrayList without using the inbuilt libraries already available to me. I understand the basics of creating the class but I'm having trouble understanding how to get the Iterator to fit into all of this. I have the following:

I have created a generic class that implements the iterable interface as such it looks something like this:

public class MyArrayList<T> implements Iterable<T> {

I've then got to create a class called MyIterator which according to the wording of the document is a stand alone class. This seems fairly straight forward I make a new class called MyIterator and have it implement the iterator interface so it looks something like this:

public class MyIterator<T> implements Iterator<T>{

My confusion lies in the following. The document says that the Iterator needs to be in it's own class, but how then do I access the data members in "MyArrayList" to fully implement hasNext() and next() for example. As the data members in the underlying array are private (as they should be) I don't see how an external class can fully implement these methods. Am I misunderstanding what is required? By separate class is it still a part of the "MyArrayList" class but defined differently?

I hope that helps, as I said I think I understand what is required of me I just am not exactly sure where my Iterator fits into all of this.

2
  • MyArrayList#iterator() returns new MyIterator(this) or somehow passes a reference to itself & thereby gives access to the data Commented Sep 4, 2013 at 9:26
  • So is MyIterator part of the MyArrayList class or does it normally go in it's own, this is where I'm confused, I don't quite know "where" the Iterator goes, if that makes any sense. I know what the iterator() method is supposed to do but I don't know where I should add the code for my custom iterator so it can create an instance of it and call return it when I need it. Commented Sep 4, 2013 at 9:32

2 Answers 2

7

While the iterator has to be a separate class *, that class will probably have some relation to your Iterable class.

It's often a nested/inner class, precisely because it needs to access the values of the class (and that's what pretty much what inner classes are made for).

Granted, if the Iterable is a List you could implement an Iterator without any "internal" access at all, but you usually still want to get access to the internals for things like checking the modCount (to throw a ConcurrentModificationException when the Iterable is structurally modified while you iterate over it ... and to prevent that exception if you modify it via the Iterator itself).

* you could implement it with your Iterable instance itself, but that would break the contract as soon as the user uses two iterators at the same time.

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

3 Comments

So essentially I want to have a set up similar to the following: public class MyArrayList<T> implements Iterable<T> { public class MyIterator<T> implements Iterator<T> { } } EDIT: Sorry I can't work out how to get code formatting working in comments.
@gRnt Exactly - iterator implementation can / should be a private class though since no-one needs to see the class. PS: put code in ` to get it formatted in text. stackoverflow.com/editing-help#comment-formatting
Thankyou I think I was confusing "it's own class" with not being a nested class!
1

You have to declare your own methods hasNext(), next(), remove(). It has to know how to iterate over your own class, how to go to next element and how to check whether next element exists.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.