views:

1081

answers:

8

I have a three-dimensional array that I want to reset to zero. It seems that there should be an easy way to do this that doesn't involve three for loops:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
            cube[i][j][k] = 0;
        }
    }
}
+9  A: 

An array will be filled with zeros after initialization:

int[][][] cube = new int[10][20][30];

This you can do also later, to reset them array to zero, it is not limited to declaration:

cube = new int[10][20][30];

Simply create a new array, it is initialized with zeros. This works if you have one place that is holding the reference to the array. Don't care about the old array, that will be garbage collected.

If you don't want to depend on this behavior of the language or you can't replace all occurrences of references to the old array, than you should go with the Arrays.fill() as jjnguy mentioned:

for (int i = 0; i < cube.length; i++)
{
   for (int j = 0; j < cube[i].length; j++)
   {
      Arrays.fill(cube[i][j], 0);
   }
}

Arrays.fill seems to use a loop in the inside too, but it looks generally more elegant.

Mnementh
Thanks. The point is that I need to reset to zero after obtaining nonzero values.
FarmBoy
then just declare a new array, the old one will be garbage collected if there's nothing referencing it
yx
+4  A: 
for (int i = 0; i < arr.length; i++){
    for (int j = 0; j < arr[i].length){
        Arrays.fill(arr[i][j], 0);
    }
}

That way you get rid of one extra loop using Arrays.fill;

Or

arr = new double[arr.length][arr[0].length][arr[0][0].length];

Unfortunately, this assumes the array is at least length >= 1.

jjnguy
Doesn't fill() just do the same thing anyway?
Marc W
It does. I just checked in the source code.
Valentin Rocher
Yeah, it really isn't a whole lot better.
jjnguy
+8  A: 

Well, it seems that you could just abandon the old array and create a new one:

int size = 10;
cube = new int[size][size][size];
bruno conde
only works if you don't have a reference to the (old) array elsewhere
Carlos Heuberger
A: 

Despite the fact that that is a 3D array, the most readable solution is often the best (best is a subjective word, you should have said best in terms of some property, and readability is usually my first choice).

If there were really only three elements in the inner loop and you wanted to emphasize that ther were three columns, you could try:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        cube[i][j][0] = 0;
        cube[i][j][1] = 0;
        cube[i][j][2] = 0;
    }
}
paxdiablo
A: 

If all the rows are the same length you could just discard the array and build a new one since the default value of int elements is zero.

cube = new int[cube.length][cube[0].length][cube[0][0].length];

Or you could do

for(int[][] tda : cube ) {
  for(int[] oda : tda) {
    java.util.Arrays.fill(oda, 0);
  }
}
+12  A: 

If you are using JDK 1.5 or higher:

    for (int[][] square : cube) {
        for (int[] line : square) {
            Arrays.fill(line, 0);
        }
    }
Yishai
+1 for great naming.
Carl Manaster
A: 

Well, you could always do this:

Arrays.fill(cube[0][0],0);
Arrays.fill(cube[0],cube[0][0]);
Arrays.fill(cube,cube[0]);

It's a little cleaner than 3 loops.

If you don't get the idea, the first "fill" fills a single dimension. The second fill copies that one dimension across two dimension. The third fill copies the two dimensions across three dimensions.

If you don't have other references to the array that you need to preserve, re-creating it as others have suggested is probably faster and cleaner.

That sure is not a good ideia. You are putting the same array (object) from cube[0][0] into cube[0][1], cube[0][2]..., and then putting the same array from cube[0] into cube[1], cube[2]...if you, for example, changes cube[0][1][2] to 1, cube[1][1][2], cube[0][0][2],... will get the same 1!
Carlos Heuberger
Ooh, good point. So much for truncated testing ...
A: 

Just create a new array and assign the variable to it... The GC will clean up the old array