Originally posted by Richard Rex:
I'm currently using jdk 1.4. I'm also using here an ArrayList that would basically hold resultsets retrieved from the database and I want this to parse the whole resultset using the ArrayList. And I am thinking of how am I gonna parse the list whether use an Iterator or simply loop for the list.
Well, without actually looking at the source code, I would expect that the iterator of the ArrayList class would just use the get method under the covers. Therefore, I would expect that, in terms of outright performance, using the get method directly would be nominally faster.
The real power of using an Iterator is that you do not need to know the underlying structure of your collection in order to get the data out of it. It sounds like you're controlling the creation of list, as well as getting the data out of it, so it's probably not a big deal. In addition, as long as you program to the interface List, rather than the concrete implementation, ArrayList, you ward off some trouble. For example, List defines the get method so, if you decide you wanted to change from an ArrayList to a LinkedList, you'd still be fine invoking get on your collection. So, using a List interface and simply looping through the objects invoking the get method would be a good way to go about this.
The question you need to ask is whether or not your collection might ever change from a plain list into something more complex, like a map or a composite. Most likely, it won't. However, if it does, using an Iterator would make your life easier, as your retrieval code won't have to change. I suspect that this isn't an issue that you necessarily need to concern yourself with, but it may be something to look at.