• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Inner classes

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yup, yup, yup. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic