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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Iterator irritation  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator

thanks for taking the time to read this
i am working through the java.util class at the moment and was wondering the following....

Iterator is an Interface, as was/is Enumerator
As such it contains only abstract methods which must be implemented
Collections' methods .Iterator() are used to obtain an Iterator object
If though the Iterator is an Interface where is the implementation of the methods performed?

Are there a hidden classes for each of the Collection types?
ie.Iterator i=new ArrayListIterator()
Thanks again
simon
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Report post to moderator
You are correct. The interator interface is a nested (inner) class implementation.
Take a look at the LinkedList.java implementation. When you call LinkedList.iterator(), you'll get a reference to an object that implements the iterator interface.
Specifically,


Why do you care that it's has to be a top-level class? As long as the class implements the iterator interface and does the correct thing, you should care less about the detail implementation. It does help to study the detail implementation.
The reason it's a nested class, is because nested class are allowed full access to the LinkedList member instance variables which is a requirement for the iterator.
-Peter
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same message to more than one forum. All future replies to the one in "Java in General (Beginner)" please.
 
A feeble attempt to tell you about our stuff that makes us money
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!