The problem is that you're trying to using Arrays.fill()
on a 2 dimensional array instead of a 1 dimensional. You can solve this by looping over the separate (1 dimensional) arrays in your 2D array.
public class Test {
public static void main(String[] args){
boolean[][] ret = new boolean[5][5];
for(boolean[] arr : ret){
Arrays.fill(arr, true);
}
for(boolean[] arr : ret){
System.out.println(Arrays.toString(arr));
}
}
}
This will output
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
See the ArrayStoreException
:
Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
And Arrays.fill(boolean[] a, boolean val)
:
Assigns the specified boolean value to each element of the specified array of booleans .
You can also use the more general public static void fill(Object[] a, Object val)
to pass in an array of boolean values like this:
public static void main(String[] args) {
boolean[][] ret = new boolean[5][5];
boolean[] tofill = new boolean[] { true, true, true, true, true };
Arrays.fill(ret, tofill);
for (boolean[] arr : ret) {
System.out.println(Arrays.toString(arr));
}
}