Take the 2-minute tour ×

I have a class Students that has a member bigTable of type ArrayList<ArrayList<Integer>>. If we imagine it a s a matrix, I would like to be able to iterate over a specific column, given as a parameter in some iterator initialization. Also, I would like the iterator to have 2 methods, getValue and getPosition. i.e. something like:

1 2 3
9 8 6
3 4 5

Student student = newStudent()
SomeType(what??) it = student.iterator(1);
it.getValue() // returns 2
it.getPosition() // returns 0
it.next() // moves to next position
it.getValue() // returns 8
it.getPosition() // returns 1

This interface is just a suggestion. I was thinking about kinda implementing the iterator pattern in Student by doing an inner class. But is it appropriate to return a reference to the inner class?? In fact if I want to pass the iterator as an argument to somebody, what type should I say it is? (inner class is not visible)...

Is there a more elegant way of accomplishing this task?

Also, just wondering, if I returned the entire row (an ArrayList, in which case Student could implement the Iterator interface) will it occupy extra memory (I mean will it be copied over or just a reference passed?)

share|improve this question
Just wrap the entire ArrayList of ArrayList in another class -- everything will be passed as a reference, so nothing will be wasted. –  Evan Knowles Mar 15 '13 at 22:47
I'm not sure I understand, sorry. –  user1377000 Mar 15 '13 at 23:02
add comment

1 Answer

up vote 1 down vote accepted

Returning an instance of an inner class is a common way of supplying an Iterator - see the java.util source code for many examples.

I think deviating from the normal Iterator interface would be unnecessarily confusing, especially if you call the method returning it "iterator".

If you want the iterator() result to let you get something with getValue() and getPosition() methods, declare an interface with those methods. I'll call it ElementData - you can think of a more meaningful name for your situation. Your iterator() method can then return Iterator<ElementData>.

Your code would become:

Student student = newStudent()
Iterator<ElementData> it = student.iterator(1);
ElementData element;
element = it.next()
element.getValue() // returns 2
element.getPosition() // returns 0
element = it.next() // moves to next position
element.getValue() // returns 8
element.getPosition() // returns 1
share|improve this answer
So the inner class would implement Iterator? –  user1377000 Mar 16 '13 at 6:24
Yes - that is the usual pattern for iterator() methods. –  Patricia Shanahan Mar 16 '13 at 7:35
Where are those 2 methods actually implemented? –  user1377000 Mar 16 '13 at 8:36
You can either make ElementData a public class, and implement them there, or make it an interface and have another nested class implement it. If the position always starts at 0 and increments by one, you could drop it and return a simple Iterator<Integer>. –  Patricia Shanahan Mar 16 '13 at 11:47
add comment

Your Answer


