• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
arun sharan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
am tryint to figure out wat this sun's java tutorial is talking about.
cant seem to put my finger on it... please explain the following para in more detail
does the paragraph say
that there can only be one active iterator for the Stack class.
or that

and also why does implementing the iterator as in inner class solve the problem...


http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

quote from the above link
"
The Stack class itself should not implement the Iterator interface, because of certain limitations imposed by the API of the Iterator interface: two separate objects could not enumerate the items in the Stack concurrently, because there's no way of knowing who's calling the next method; the enumeration could not be restarted, because the Iterator interface doesn't have methods to support that; and the enumeration could be invoked only once, because the Iterator interface doesn't have methods for going back to the beginning. Instead, a helper class should do the work for Stack.
"

byeo
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

as per my understanding, to give seperate iterator to each object of stack class they r making iterator as inner class.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If "Stack implements Iterator" then a stack is-an Interator. Imagine the code:

Now ask yourself these questions:
1. What happens if you call this method twice in a row?
2. What happens if two threads concurrently execute this code on the same statck?

The solution is for Stack (actually, any Collection) to have the method:

public Iterator iterator()

*How* this iterator is implemented is not important. What is important is how it behaves. For example, the previous method can now be written:

But if you insist on seeing an implementation of the iterator, it will probably be an inner class of Stack (or ArrayList) because then it can access Stack's array implementation.
 
arun sharan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot ... i think i understand it now...
i was not thinking about it from the perspective of threads.

byeo
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!