Topic
  • 11 replies
  • Latest Post - ‏2013-04-04T15:15:34Z by SystemAdmin
SystemAdmin
SystemAdmin
4672 Posts

Pinned topic Loop a List

‏2003-03-08T05:58:23Z |
What is the different/benefit to access a list using for loop and iterator? e.g.

List l = new ArrayList();
for(int i=0; i<l.size(); i++) {
Object obj = l.get(i);
}

List l = new ArrayList();
Iterator i = l.iterator();
while (i.hasNext())
Object obj = i.next();
Updated on 2013-04-04T15:15:34Z at 2013-04-04T15:15:34Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2003-03-08T22:56:48Z  
    I think the differences are obvious. Why don't you create a timing example to see which one is faster.
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2003-03-15T04:50:35Z  
    I think the differences are obvious. Why don't you create a timing example to see which one is faster.
    So the difference is only the performance?
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2003-03-16T01:43:47Z  
    So the difference is only the performance?
    Each allows you to visit each element of the list. The difference is not only performance. But if all you wanted to do was do something with each element of the list, than performance IS the only difference, as both allow you to do that.
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2003-03-26T18:09:30Z  
    If you use iterator your code is more generic and flexible( not specifc to list it can be any collection even a HashSet). It allows you code to change much easyly later.
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2003-05-06T13:24:32Z  
    There are some differences, for example, you can easily delete the current element using an Iterator's remove() method, which required you take care of the right bound of the incremental loop. Effectively and semantically, both are identical when used as asked.

    Consider using the following code (see java.util.RandomAccess for details)

    code
    List l;
    if(l instanceof RandomAccess) {
    for(int i = 0;i<l.size();i++) {
    Object o = l.get(i);
    }
    }
    else {
    for(Iterator i = l.iterator();i.hasNext();) {
    Object o = i.next();
    }
    }
    [/code]

    Regards
  • gr8yash
    gr8yash
    6 Posts

    Re: Loop a List

    ‏2012-08-20T13:29:13Z  
    One of the main difference between looping through ArrayList using lop and iterator is that. Iterator will allow you to remove element at the time or traversing but in loop you will get ConcurrentModificationException. Refer
    Java ArrayList Iterator Example
    for more details.
  • gr8yash
    gr8yash
    6 Posts

    Re: Loop a List

    ‏2012-08-20T13:29:24Z  
    One of the main difference between looping through ArrayList using loop and iterator is that. Iterator will allow you to remove element at the time or traversing but in loop you will get ConcurrentModificationException. Refer
    Java ArrayList Iterator Example
    for more details.
  • gr8yash
    gr8yash
    6 Posts

    Re: Loop a List

    ‏2012-08-20T13:30:16Z  
    One of the main difference between looping through ArrayList using loop and iterator is that. Iterator will allow you to remove element at the time or traversing but in loop you will get ConcurrentModificationException. Refer
    Java ArrayList Iterator Example
    for more details.
  • ThomasErdbrik
    ThomasErdbrik
    2 Posts

    Re: Loop a List

    ‏2012-08-21T13:18:29Z  
    Loop is best developing any program.

    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile Apps Development[/URL]||
    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile Application Development[/URL] ||
    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile App Development[/URL]
  • ThomasErdbrik
    ThomasErdbrik
    2 Posts

    Re: Loop a List

    ‏2012-08-21T13:19:19Z  
    Loop is best developing any program.

    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile Apps Development[/URL]||
    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile Application Development[/URL] ||
    URL=http://www.mindinventory.com/mobile_apps_dev.phpMobile App Development[/URL]
  • SystemAdmin
    SystemAdmin
    4672 Posts

    Re: Loop a List

    ‏2013-04-04T15:15:34Z  
    for is used to get elements using condition
    iterator not used condition, it can get elements till false list