• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections - A question on iterator.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we have iterators in subclasses of List interface(i.e ArrayList, Vector and LinkedList), when we can access the elements using index number?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you have a List of 100 elements. Do you want to retrieve all 100 elements using index method...

that is not true right. So iterator will give you fast retrieving of data from List
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main reason is that when you use iterator,you never end up in any NoSuchElementException which usally arises when you use direct index to fetch a element which is not present.Its a safer way to retrive elements.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also K & B say that iterator was included in previous versions of java so its present for oldies ..no seriously book says that.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suppose you have a List of 100 elements. Do you want to retrieve all 100 elements using index method...that is not true right.
So iterator will give you fast retrieving of data from List



Main reason is that when you use iterator,you never end up in any NoSuchElementException which usally arises when you use direct index to fetch a element which is not present.Its a safer way to retrive elements.



Well, I guess accessing by index is much faster than using Iterator and secondly, it does not throw NoSuchElementException. It throws IndexOutOfBoundsException. Please do not reply if you are confused.

My question still remains unanswered.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I say get(index) method is better than iterators, I consider only Vector and ArrayList classes for n number of records. This does not includes LinkedList.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ritu,

Please remember that we're all volunteering our time here at the ranch! We encourage everyone to participate in discussions of the questions that are posted here. To that end I think that this statement:


Please do not reply if you are confused.



Is probably not in the spirit of JavaRanch. Everyone in this forum is at some stage of learning Java, some know more than you and some know less, but this forum is a place for open discussions, and all sincere questions and answers must be treated with respect!

Thanks!

Bert
 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic