File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ListIterator or list(0) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ListIterator or list(0)" Watch "ListIterator or list(0)" New topic
Author

ListIterator or list(0)

Girish Bal
Ranch Hand

Joined: Jun 23, 2005
Posts: 52
I dont know whether its an advanced topic. I have a doubt.

Which is better? Using ListIterator or list(0), list(1) etc.

For eg: I have a list. I want to iterate through it in a for loop and display the details.


Which is better?

[ December 14, 2005: Message edited by: Girish Bal ]
[ December 14, 2005: Message edited by: Girish Bal ]

Girish B
SCJA 1.0 (86%)
SCJP 1.4 (91%)
OCPJWCD (86%)
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

As with most programming questions that ask "which is better", the answer is "it depends".
Both techniques do the same thing: move from one end of the list to the other. A for loop is quick, dirty and very familiar to those who know other languages. An iterator is more "object oriented", leaving the implementation details to someone else. It also protects the list from concurrent modification, which could cause a big problem in a for loop.
So ask yourself which is more appropriate in your circumstance. Personally, I rely on iterators almost exclusively, but that's more of a style choice than a technical decision.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
I will also take in account that with the time you will probably get in touch with collections as a generalization to lists, collections are usually iterated. So if you learn to use the iterator it will help you in the future.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
If you are using Java 1.5, you might also look at For-Each. If you just need to move through the elements, then for-each works great. If you multiple iterators to access an element (i.e. multidimensional lists), or want to delete items, then you can't use the for-each construct.



Cheers,
[ December 14, 2005: Message edited by: Tom Blough ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Grant Gainey
Ranch Hand

Joined: Oct 16, 2005
Posts: 65
I'd usebecause I like the way my code looks using the new for-loops.

But that's just me...

Grant

[Edited to add the generic]
[ December 14, 2005: Message edited by: Grant Gainey ]

In Theory, there is no difference between theory and practice.<br />In Practice, there is no relationship between theory and practice.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Originally posted by Grant Gainey:
[Edited to add the generic]


Great minds think alike
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And for what it's worth, JDK 5's enhanced for loop is basically equivalent to using an Iterator and casting the result. But nicer looking. I pretty much always use enhanced for if possible, and an Iterator (maybe ListIterator) otherwise.

Note that if you ever replace your ArrayList with a LinkedList (or if you don't know what kind of List you have) then using get() can lead to very poor performance. While using an Iterator gives consistently good performance - perhaps not always the very best performance possible, but usually close enough that it doesn't matter. It's the horrible performance of get() on a LinkedList that you want to avoid. Furthermore you may want to replace your List with a Set or other Collection instead - get() is not available here, but an Iterator still works fine.
[ December 14, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Good point Jim. Just as we strive for abstraction in our class design, we should also look to abstraction in our algorithm design.

Cheers,
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Note that if you ever replace your ArrayList with a LinkedList (or if you don't know what kind of List you have) then using get() can lead to very poor performance. While using an Iterator gives consistently good performance - perhaps not always the very best performance possible, but usually close enough that it doesn't matter. It's the horrible performance of get() on a LinkedList that you want to avoid. Furthermore you may want to replace your List with a Set or other Collection instead - get() is not available here, but an Iterator still works fine.


That's precisely why I insist on using Iterators. If you don't you run the risk of incurring massive performance hits if the implementation changes whereas an Iterator is fairly consistent. I can't think of any situation that performance was so crucial that the marginal gains from not using an Iterator outweighed the concerns. I suspect that if I encountered a situation where performance was that critical I wouldn't be using a Collection to begin with as I would want very precise control over the implementation.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I prefer to use Iterator. I don't think list(0) compiles. Iterator should sometimes be faster as well. But list.get(0) also will not fail with concurrent modification exception, though it may with ArrayIndexOutofBoundsException.
Girish Bal
Ranch Hand

Joined: Jun 23, 2005
Posts: 52
thanx guys.. i'm using java 1.3.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ListIterator or list(0)