#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Every element of a 3D array

edited October 2016

I know how to do this the long way, visit every element of a 3D array, but I am wondering if there is a shortcut and optimized way to do the following:

```for (int i = 0; i < CPS; i++) { for (int j = 0; j < CPS; j++) { for (int k = 0; k < CPS; k++) { box[i][j][k].draw(); }}}```

I am looking for something like this:

`for every element of box[][][] {box[][][].draw;`

OR

`box[(int i = 0; i < CPS; i++)][(int j = 0; j < CPS; j++)][(int k = 0; k < CPS; k++)].draw();`

OR the best yet would be

`box[][][].draw;`

Any and all suggestion would be appreciated.

Tagged:

## Answers

• There isn't an automatic out-of-the-box way to do this in Processing.

You might google something like "java flatten arrays" for a ton of results.

But just using the nested `for` loop is probably the easiest option. Maybe put this in a function so it looks like a one-liner to the rest of your code.

• make a class Cube and say

cube.draw();

• ``````// forum.Processing.org/two/discussion/18708/
// every-element-of-a-3d-array#Item_3

// GoToLoop (2016-Oct-25)

final int DIM = 3;
final PVector[][][] three = new PVector[DIM][DIM][DIM];

// Fill 3D array:
for (PVector[][] two : three)  for (PVector[] one : two)
for (int i = 0; i < one.length; one[i++] = PVector.random3D(this));

// Visit 3D array:
int i = 0;
for (PVector[][] two : three)  for (PVector[] one : two)
for (PVector vec : one)  println(i++, vec);

exit();
``````
• Thank you GoToLoop, let me try this and doo some metrics and see how it goes.

Sign In or Register to comment.