views:

259

answers:

3

How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are?

Why I would use an iterator instead of using the get() methods of the Arraylist

+3  A: 

An ArrayList is a particular kind of List. It's a data structure, like a tree or a map or a list.

An Iterator is an example of a design pattern for walking through a data structure. You don't navigate a list the same way you would a binary tree, but an Iterator provides an interface that describes what's common to all data structure navigators.

So you might use an Iterator to walk through an ArrayList. They aren't the same thing. One's a navigator, the other's a container.

So why would you prefer an Iterator over just using the get() method for ArrayList?

Not all collections have a get() method (e.g., Collection and Set and binary tree). If you need to switch from an ArrayList to one of those data structures you have to rewrite all the code that calls get() as well.

But if you return an Iterator, clients of the code don't have to know that you've changed implementations.

If you use a get(), you use the implementation that's given to you. If you use an Iterator interface, you can swap implementations to do things like smart proxying without affecting clients.

It's about hiding details regarding navigation from clients of a class. If you just use ArrayList it might not matter much. But if you're designing classes of your own, you might find that Iterator gives you a nice way to expose behavior without giving away private details.

duffymo
A: 

Review this tutorial from Sun on how to use Iterators on classes implementing the Collection interface (including of course, ArrayList).

This can give you some quick hands-on examples on how they are used, which may help to visualize their differences.

Dan
+1  A: 

An ArrayList is an actual data structure, an implementation of the List interface. An Iterator is just an interface that allows you to navigate through any data structure (as long as an Iterator is available for that data structure).

In other words, an ArrayList is an actual list of object references (or primitives) stored physically in an array. ArrayList is an "implementation" of the List interface, meaning it provides implementations of all the methods that are applicable to a List, such as add(object), remove(object), get(index), etc.

Iterator is a more generic way to navigate through any data structure, be it a Set, a List, whatever. One important point is that it lets you navigate through every element in the data structure once, and then you're done. From the documentation you can see that Iterator prescribes two methods, next() and hasNext(). next returns the next element in the underlying data structure, and hasNext lets you know if there even is a next element in the underlying data structure. Several data structures, ArrayList included, can provide you with an Iterator.

Why I would use an iterator instead of using the get() methods of the Arraylist?

Well, like many interfaces, Iterator allows you to do the same kind of thing no matter what the underlying implementation. If I want to "iterate" through some data structure, I can either

a) write code that is specifically geared toward the data structure (such as an ArrayList) that I will have to change later if I change the data structure to something else (such as a HashSet) or

b) obtain an Iterator from the data structure, and use the same hasNext/next technique that will work even if I change the data structure to something else.

FYI, if you aren't too familiar with the words "interface" and "implementation" you should probably do a Google search on "Java interface".

Mirage114