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

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

### Solution:1

If you are using JDK 1.5 or higher:

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

### Solution:2

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

### Solution:3

An array will be filled with zeros after initialization:

``int[][][] cube = new int;  ``

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

``cube = new int;  ``

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.

### Solution:4

``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.length][arr.length];  ``

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

### Solution:5

Well, you could always do this:

``Arrays.fill(cube,0);  Arrays.fill(cube,cube);  Arrays.fill(cube,cube);  ``

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.

### Solution:6

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;           cube[i][j] = 0;           cube[i][j] = 0;       }  }  ``

### Solution:7

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.length][cube.length];  ``

Or you could do

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

### Solution:8

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
Previous
Next Post »