aspose file tools
The moose likes Beginning Java and the fly likes Why to use iteration instead of size for list? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why to use iteration instead of size for list?" Watch "Why to use iteration instead of size for list?" New topic
Author

Why to use iteration instead of size for list?

par dhar
Greenhorn

Joined: Nov 16, 2006
Posts: 11
Hi all,

Sorry to ask such a basic queation, but can anybody please let me know why it is adviced to use iteration to loop thro' list elements instead of size() method?

Regards
Parag
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

To clarify your question, here's a piece of code:

The answer is this: There are different implementations of interface List, for example ArrayList and LinkedList. The main difference between these implementations is that for one, some operations are efficient and other operations are less efficient and vice versa.

For example, random lookup (using get(...)) is efficient on an ArrayList, but not on a LinkedList (because it has to walk through the whole list to get at the element you are looking for). So the second loop (using "size()") is very inefficient when the List is a LinkedList.

Using an iterator is better, because the iterator that you get from the list "knows" what the most efficient way is for looping through the list. (The list itself supplies the iterator, so it can give you an iterator that is efficient for that particular implementation of List).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9955
    
    6

Also, by using an iterator, you are using the same interface for every kind of list. I can always use the same code, no matter what kind of implementation the unerlying list has.

there are cases where i don't care what the underlying implementation is - i just want a list. i want to be able to loop over it.

if i do it your way, i am locking myself into a specific algorithm. if the list changes how it's designed, or somebody come up with a brand new kind of list, you might be out of luck until you can re-factor your code to handle the changes.

using an iterator, i have a common way to go through ANY kind of list, as long as that list supports the iterator interface. maybe we have 12 different lists we need to iterate through, one an array list, one a map, one a set... i can write



you would have to write:



my code lets anybody add any new kind of list any time they want, or change the underlying implementation of an existing list, and i don't care. my code doesn't break.

yours can't handle a new type of list without you changing your code. and if somebody changes how an existing list works, you might be out of luck again.


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why to use iteration instead of size for list?
 
Similar Threads
get fifth element from last for a Singly linked list
'outer:' , 'inner:' with "break" /"continue"
not able to display arrayList properly using html:select
sub list inside a list
How to remove duplicate element from List