• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot fit generic class to use with foreach  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

I`ve implemented Stack on the base of the LinkedList.
It works as i expect it to work, but then i tried to made it compatible with foreach loop.
I`ve implenmented Iterable and Iterator.
Code compiles and works fine, but does not return any output. It looks like while working with foreach, next() is not called at all. If i`m getting iterator from instance and try to do iterator.next(), i get output as expected.
Any hint what i have missed? thanks!

 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Daniel,

it is simply because your Integers are stored in the LinkedList 'LL',
so line 17 does not work.

What you can do, is to add a 'printAll' method, that simply
prints out all the Integers in 'LL', or you could supply a
public method 'getLL' and return a reference to 'LL' (which
of course would spoil the private nature of LL), or
simply use the Iterator, since that is what your class
implements!

Greetz,
Piet
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, a matter of style: class names should start with an upper-case letter - I would call the class GenericStack instead of genericStack. Variable names should start with a lower-case letter, so your variable GSInteger should have been named gsInteger.

Then about the design: It's a little weird that your class implements both Iterator and Iterable. It should implement Iterable, but not Iterator, since the stack is itself not an iterator - it only knows how to provide an iterator.

An iterator is an object that helps you iterate over a collection. It is a kind of "pointer" into a collection, and it has some state (it remembers where it was in the collection). Note that there can be multiple iterators at the same time for one collection. So, it's wrong for the collection itself to be the iterator.

you should implement a separate iterator class, for example class GenericStackIterator<T> that implements Iterator<T>, and then in the iterator() method of your class GenericStack you should return a new instance of GenericStackIterator.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at your hasNext method. An iterator has-next if the collection is NOT empty. Right?

General advices:
Class name should start with capital letters.
If your remove method does nothing let it throw UnsupportedOperationException.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Daniel,

it is simply because your Integers are stored in the LinkedList 'LL',
so line 17 does not work.

What you can do, is to add a 'printAll' method, that simply
prints out all the Integers in 'LL', or you could supply a
public method 'getLL' and return a reference to 'LL' (which
of course would spoil the private nature of LL), or
simply use the Iterator, since that is what your class
implements!

Greetz,
Piet


Wait, i thought that foreach will use next() to get something and will assign this something to loop variable, in this case, i. Am i thinking wrong ?
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Take a look at your hasNext method. An iterator has-next if the collection is NOT empty. Right?

General advices:
Class name should start with capital letters.
If your remove method does nothing let it throw UnsupportedOperationException.


Wow! Thanks, Hawk Eye, i overlooked this logic twist with hasNext().

Is remove() method removes value, after next() fetched it ?
I mean does foreach gets value by calling next() and then triggers remove() to eliminate already fetched value?


Jesper de Jong wrote:First of all, a matter of style: class names ...
Then about the design...


Thanks for pointing this out. I still forget about code conventions rules.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:Is remove() method removes value, after next() fetched it ?

What does javadoc say about it?

Daniel Gurianov wrote:I mean does foreach gets value by calling next() and then triggers remove() to eliminate already fetched value?

Foreach uses hasNext and next methods. It never calls remove.

In your class next method removes elements. It is very odd behavior for an iterator.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote: . . .
In your class next method removes elements. It is very odd behavior for an iterator.
Actually, it is not permissible behaviour because it breaches the general context of Iterator#next, which does not remove elements.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!