PDA

View Full Version : Modifying iterators


Cysion
08-15-2007, 10:07 AM
Aloha people,

A question concerning modifying iterators in java, and if it weren't for the fact that I already waisted few hours on it, then I'd really like your help :p

Ok, this is the problem: we have a virtual dungeon, all squares in that dungeon are saved in a vector. All these squares have certain coordinates (x,y,z). Now we need to make an iterator in dungeon that iterates over all these squares and returns the squares in the following order: x in ascending order, then y in ascending order and finally z in ascending order. So the square with the lowest x has to be returned first. E.g.: (1,0,0), (1,1,0) (2,1,0) (2,1,9) etc....

Anyone an idea how to coop witt all of this? Thanks in advance :D

brad211987
08-15-2007, 03:37 PM
Can you expand on the order you want these to be listed? I'm not completely catching on from "(1,0,0), (1,1,0) (2,1,0) (2,1,9)"

Cysion
08-15-2007, 04:25 PM
Well, just imagine as a 3dimensional array, where you first iterate the first level> in this level you iterate the first row> and here you first iterate the first square. And then going on till you iterated all the levels (thus all the squares)

brad211987
08-15-2007, 05:34 PM
You could do a seperate class for your iterator, initializing x, y, and z to 0.

in your next() method, you can set up how you want to navigate around the cube. For example if you had a 10x10x10 cube:

if x and y both = 9, increment z, and set x and y to 0
if x <9 and y = 9, increment x and set y to 0
etc....

probably would look nicer using a switch block, but thats the general idea.