20

How to fill a multidimensional array?

int[][] array = new int[4][6]; 
Arrays.fill(array, 0);

I tried it, but it doesn't work.

Argote
  • 2,155
  • 1
  • 15
  • 20
alphachap
  • 201
  • 1
  • 2
  • 3
  • Did any of the solutions given work for you? – Argote Mar 11 '11 at 18:32
  • 1
    possible duplicate of [Arrays.fill with multidimensional array in Java](http://stackoverflow.com/questions/7118178/arrays-fill-with-multidimensional-array-in-java) – om-nom-nom Aug 26 '12 at 15:17
  • If the array doesn't need to be a jagged array, you could create a single dimensional array (of size width * height) and just access it via `int index = (y * width) + x;` -- you could even create a class that just exposes get/set methods that take x and y as separate arguments. Then you could fill the whole array without any looping. – BrainSlugs83 Oct 06 '14 at 21:10
  • Possible duplicate of [Syntax for creating a two-dimensional array](http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – StarWind0 Aug 18 '16 at 17:27

5 Answers5

31

Here's a suggestion using a for-each:

for (int[] row : array)
    Arrays.fill(row, 0);

You can verify that it works by doing

System.out.println(Arrays.deepToString(array));

A side note: Since you're creating the array, right before the fill, the fill is actually not needed (as long as you really want zeros in it). Java initializes all array-elements to their corresponding default values, and for int it is 0 :-)

aioobe
  • 413,195
  • 112
  • 811
  • 826
13

Try this:

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

I haven't tested it but I think it should work.

Argote
  • 2,155
  • 1
  • 15
  • 20
2

Since array is really an array of arrays, perhaps you can try looping over each row and doing fill for each one individually.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

First, note that 0 is the default value for int arrays, so you don't have to fill something with 0.

If you want your array to stay truly multidimensional, you'll need a loop.

public static void fill(int[][] array, int element) {
    for(int[] subarray : array) {
        Arrays.fill(subarray, element);
    }
}

If you only need a 2D-array filled with the same element and don't want to change the subarrays later, you can use this trick:

public static int[][] create2DArray(int length, int subLength, int element) {
    int[] subArray = new int[subLength];
    Arrays.fill(subArray, element);
    int[][] array = new int[length][];
    Arrays.fill(array, subArray);
    return array;
}

This works analogously for higher-dimensional arrays.

Michael Alan Huff
  • 3,462
  • 3
  • 28
  • 44
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
-3

See this way:

Arrays.fill(array[0], 0);
Arrays.fill(array, array[0]);
GreMal
  • 221
  • 2
  • 5
  • 1
    This makes every sub-array, the exact same array, so that if you change `array[0][0]`, you also change `array[1][0]`, `array[2][0]`, ... to the same value. Not a good solution. – trincot Aug 18 '16 at 18:01