Tutorial :What's the best way to set all values of a three-dimensional array to zero in Java?


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;          }      }  }  


If you are using JDK 1.5 or higher:

    for (int[][] square : cube) {          for (int[] line : square) {              Arrays.fill(line, 0);          }      }  


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];  


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.


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;


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

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


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.


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 there were three columns, you could try this:

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;       }  }  


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);    }  }  


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »