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.

4
  • Did any of the solutions given work for you? Commented Mar 11, 2011 at 18:32
  • 1
    possible duplicate of Arrays.fill with multidimensional array in Java Commented Aug 26, 2012 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. Commented Oct 6, 2014 at 21:10
  • Possible duplicate of Syntax for creating a two-dimensional array Commented Aug 18, 2016 at 17:27

5 Answers 5

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 :-)

Sign up to request clarification or add additional context in comments.

2 Comments

for instance variables java does(automatic initalization) but not for local variables So above the above code adds importance in some cases
@JoelDeWitt, why? :)
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.

1 Comment

This is using fill for a regular int array only, hence the for loop.
2

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

Comments

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.

Comments

-3

See this way:

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.