Author
How to iterate a Set (2 steps once)
Andrei Antonescu
Ranch Hand
Joined: Jul 08, 2010
Posts: 75
Hello,
Can you please tell me if there is any way of iterating over a Set making 2 steps at once?
I would like the equivalent of :
but for a Set collection.
Thanks in advance
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
what's wrong with using an iterator, and just calling next() twice?
Never ascribe to malice that which can be adequately explained by stupidity.
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Of course ensuring that there is an even number of elements, or by checking hasNext() before calling next again.
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
Every element in a Set is distinct, so my question is why would you be skipping over elements? And if the elements don't matter why cant you just use an array?
Hunter
"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
If you have named your variables correctly, you are not iterating a Set but a List.
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Jul 30, 2010 17:01:16
0
"equivalent of"
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
David Newton wrote: "equivalent of"
Yes, I missed that. But Fred has already given a good answer.
Andrei Antonescu
Ranch Hand
Joined: Jul 08, 2010
Posts: 75
Hello,
Thank you all for your replies. I will call next() twice.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
Maybe better to try thisOtherwise you may suffer an exception.
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
That's what I already said.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
Rob Prime wrote: That's what I already said.
Yes, but I only ever notice your posts when they are 5 seconds ahead of mine, not 3 days
subject: How to iterate a Set (2 steps once)