• Post Reply Bookmark Topic Watch Topic
  • New Topic

iterate from middle of an ArrayList  RSS feed

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a standard way to iterate from the middle of an ArrayList, in which all the items get iterated through after the item, and then all before the item (but not the item)?

It seems like I need to get an iterator, but when I iterate back through the beginning of the list should I be getting a second iterator or resetting the first somehow?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:ArrayList.html#listIterator(int)


It doesn't iterate back through the beginning of the list.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:
Pawel Pawlowicz wrote:ArrayList.html#listIterator(int)


It doesn't iterate back through the beginning of the list.



Please elaborate the issue -- as list iterators can iterate in both directions.

Henry
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a circle of players in a game. Whoever goes first in a round is not determined by the order, but each other player's turn is. I came up with some code that works so far:



It all seems a bit messy though.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart form the answer ,
using two iterators over one ArrayList, is not a good practise.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:Apart form the answer ,
using two iterators over one ArrayList, is not a good practise.


Yeah it doesn't seem like it is, but as I said before, a single ListIterator does not work. The values should be though of as being in a circle. If I have one value in the circle I want to make a complete trip around the circle, getting back to that value (but not using it).
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any specific reason to use an iterator?
If K is the player to play first, then a simple loop

would do the trick.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea with the %. A little tricky working out the condition though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!