This is because a double[][] is an array of double[] which you can't assign 0.0 to (it would be like doing double[] vector = 0.0). In fact, Java has no true multidimensional arrays.

As it happens, 0.0 is the default value for doubles in Java, thus the matrix will actually already be filled with zeros when you get it from new. However, if you wanted to fill it with, say, 1.0 you could do the following:

I don't believe the API provides a method to solve this without using a loop. It's simple enough however to do it with a for-each loop.

double[][] matrix = new double[20][4];

// Fill each row with 1.0
for (double[] row: matrix)
    Arrays.fill(row, 1.0);
Answer from aioobe on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › java › fill 2d array in java
How to Fill a 2D Array in Java | Delft Stack
March 11, 2025 - Java provides a built-in method called Arrays.fill() that can be used to fill an entire array or a specific range of an array. This method can also be applied to 2D arrays, but you will need to use it in conjunction with a loop.
Discussions

Filling a 2D array in Java - Stack Overflow
I am new to java and I am struggling immensely! I've written the following code but keep getting errors. All I am trying to do at the moment is fill a 5x5 matrix with the letter A. Here's what I ha... More on stackoverflow.com
🌐 stackoverflow.com
How do you fill a 2D array with "cells"/coordinates?
It's very hard to understand what you want and I didn't even understand it myself. I'll try to answer what I think might be your problems, but if I'm incorrect don't hesitate to reply to my answer. In case you just want to have a grid: int[][] grid = new int[10][10]; // All the entries are automatically // instantiated to 0, because primitives always have a default value they are // initialized to. /* You can also use any other primitive datatype: */ char[][] grid = new char[10][10]; /* You can also use any other non-primitive datatype: */ String[][] grid = new String[10][10]; /* The big difference here is that the default value of all of these is 'null' */ /* You can even use your own classes: */ record Person(String name){} Person[][] grid = new Person[10][10]; /* Obviously now the problem is that we want to store objects in there, not just 'null' and therefore we need to fill the grid (let's use our Person grid for that): /* //To achieve what we want we need a nested for-loop for(int y = 0; y < grid[0].length; ++y) { for(int x = 0; x < grid.length; ++x) { //Let's assume we have a namelist where we can get a new name/String by 'namelist.next()' Person = new Person(namelist.next()); } } /* You can easily access the grid at any time at any place, like f.e. so: */ System.out.println(grid[0][0]); Person currentPerson = grid[9][3]; //Please keep in mind that indexing starts from 0 More on reddit.com
🌐 r/learnjava
1
4
October 2, 2021
[Java] How to fill an 2D array with different unique numbers?
If you wanted to fill an array with unique values, the most straightforward approach is: For index i, generate a random value. Check the array, up to index i, to see if the array already contains that value. If yes, go back to step 1. Put that value in the array at index i, increment i, and go back to step 1. I think more efficient approach might be: If you know all the unique values in the set (1, 2, 3, and 4), add those to a temporary array. Then when you're creating your 2D array, when you're creating a row, you can just shuffle the values in the temporary array, and then copy that array to your row array. More on reddit.com
🌐 r/learnprogramming
12
3
September 16, 2012
java - Can I use Arrays.fill with a 2d array? If so how do I do that? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Closed 11 years ago. Can you use Arrays.fill on 2d arrays? When I type it, it says there is an issue with fill Here is my code. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I use the `Arrays.fill()` in Java?
To use the `Arrays.fill()` method, you simply pass the array you want to fill and the value you want to fill it with to the method. For example, the following code fills an array of integers with the value `10`:
🌐
scaler.com
scaler.com › home › topics › arrays.fill() in java with examples
Arrays.fill() in Java with Examples - Scaler Topics
What are some limitations of the `Arrays.fill()` in Java method?
The `Arrays.fill()` method cannot be used to fill arrays with multiple values. To do this, you can use a for loop or the Stream API.
🌐
scaler.com
scaler.com › home › topics › arrays.fill() in java with examples
Arrays.fill() in Java with Examples - Scaler Topics
What are the benefits of using the `Arrays.fill()` method?
There are several benefits to using the `Arrays.fill()` method:
🌐
scaler.com
scaler.com › home › topics › arrays.fill() in java with examples
Arrays.fill() in Java with Examples - Scaler Topics
🌐
Scaler
scaler.com › home › topics › arrays.fill() in java with examples
Arrays.fill() in Java with Examples - Scaler Topics
April 9, 2024 - A 2D array can be thought of as an array of arrays. So when we traverse through the array, each element in the 2D array is also an array of 1 Dimension. We can use Arrays.fill() method to fill each of these arrays.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-fill-java-examples
Arrays.fill() in Java with Examples - GeeksforGeeks
Arrays.fill() is a method in java.util.Arrays class which assigns a specified value to each element of an entire array or a specified range within the specified array.
Published   April 13, 2026
Top answer
1 of 2
4

Arrays in Java are zero-based, which means they start at index zero, and range until index array.length-1.

Your code starts the row and column at 1—which means you're skipping the initialization of row/column 0. That's probably where at least some of the problems are coming from, since you're using your 5x5 array (rows/columns 0,1,2,3,4) as a 4x4 array (rows/columns 1,2,3,4).

There's also the fact that your Encrypt method doesn't actually make any assignments to the array. You probably want to initialize it like this:

// NOTE: changed return type to void -- this is a side-effect-only method!
public void Encrypt(char Encrypt[][])
{
    // NOTE: use single-quotes for chars. double-quotes are for strings.
    char aChar = 'A';
    // NOTE: changed the starting loop values from `1` to `0`
    for (int row = 0; row < Encrypt.length; row++)
    {
        // NOTE: technically Encrypt.length works here since it's a square
        // 2D array, but you should really loop until Encrypt[row].length
        for (int column = 0; column < Encrypt[row].length; column++)
        {
            // NOTE: set each entry to the desired char value
            Encrypt[row][column] = aChar;
        }
    }   
}

There are several issues with your original code. Look at the NOTE entries in the comments for individual explanations.

2 of 2
2

You are missing the most crucial part of what you are trying to accomplish.

Where are you setting your matrix to the letter A?

Change your Encrypt function to the following:

//filling the array with the letter A
public void Encrypt(char arr[][])
{
    //char[] alpha = alphabets.toCharArray;

    //declaring variable to fill array with A           
    char aChar = 'A';

    for (int row = 0; row < arr.length; row++)
    {
        for (int column = 0; column < arr[row].length; column++)
        {
            arr[row][column] = aChar;
        }
    }   
}   
🌐
Sololearn
sololearn.com › en › Discuss › 1691924 › how-one-can-fill-a-multidimensional-array-in-java
How one can fill a multidimensional array in Java? | Sololearn: Learn to code for FREE!
February 17, 2019 - Multidimensional Arrays You can get and set a multidimensional array's elements using the same pair of square brackets. Example: int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; // 4 The above two-dimensional ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnjava › how do you fill a 2d array with "cells"/coordinates?
r/learnjava on Reddit: How do you fill a 2D array with "cells"/coordinates?
October 2, 2021 -

Just to be clear, I don't want to create a point object nor do I want to something like i + j etc.

For example, I want to have a 10 x 10 "grid" in order to work with union-find / disjoint set to generate a maze so I need the numbers, not objects. I may be overthinking this but how in the world does one create a grid to access a cell? Like, if I say maze[1][2], I would conceptually (not literally) expect to get 2,3 if that makes sense, but not an actual coordinate output of (2,3). I want to be able to unionize the cells to create a path.

🌐
Reddit
reddit.com › r/learnprogramming › [java] how to fill an 2d array with different unique numbers?
r/learnprogramming on Reddit: [Java] How to fill an 2D array with different unique numbers?
September 16, 2012 -

Hello reddit, What I want to do is basically to fill a 2D array with unique numbers in every row and column. The numbers should be from 1 to 4. And as you may have guessed the array is 4x4.

I have the basics set up with a nested for loop,looping trough the array. Giving it ,at the moment, random but not unique values. I can not really wrap my head around the logic of checking the the values already in the array.

How would I proceed to fill the rows and columns with unique values?

Example of output:

  • 1 2 3 4

  • 2 3 4 1

  • 3 4 1 2

  • 4 1 2 3

Any help is appreciated!

🌐
Coderanch
coderanch.com › t › 697695 › java › Filling-dimensional-arrays
Filling two-dimensional arrays [Solved] (Beginning Java forum at Coderanch)
August 9, 2018 - (the dice have original colours but since I can't mix strings and numbers red dice will be first row 0, and blue second row 1 etc. Hope this is clear and thanks in advance. Marcus ... The answer is yes. Java doesn't have true 2-dim arrays, but what it has is better.
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-fill-method-explained-5001c17914eb
Java’s Arrays.fill() Method Explained | Medium
October 14, 2024 - This article will walk you through the basics of how the method works, some of its common use cases, and examples that demonstrate its versatility in real-world scenarios. The Arrays.fill() method in Java provides a quick and easy way to fill ...
🌐
CodeSpeedy
codespeedy.com › home › arrays.fill() in java with examples
Arrays.fill() in Java with examples for 1D, 2D and 3D arrays - CodeSpeedy
June 13, 2020 - Here in this tutorial, we are going to apply fill() function on 1-D, 2-D, and 3-D arrays in Java.
🌐
Javatpoint
javatpoint.com › java-arrays-fill
Java Arrays Fill - Javatpoint
Java Arrays Fill with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Sololearn
sololearn.com › en › Discuss › 1539431 › how-do-i-get-the-code-to-fill-in-2d-array
How do I get the code to fill in 2d array? | Sololearn: Learn to code for FREE!
I get you want an array of 512 values filled out with the 9 bit pattern that equals the value. I don't understand what you are trying to do with the 19. Are you looking for all of the patterns that have their last 5 bits ending in 10011 (i.e. 19)? Without understanding what you want to do, I can not make it happen. ... It is a hexadecimal value of 512. I use hex to show bits when I am using it as such. https://www.javamex.com/tutorials/conversion/decimal_hexadecimal.shtml
🌐
Java67
java67.com › 2014 › 10 › how-to-create-and-initialize-two-dimensional-array-java-example.html
How to declare and Initialize two dimensional Array in Java with Example | Java67
If you know how to create a one-dimensional array and the fact that multi-dimensional arrays are just an array of the array in Java, then creating a 2-dimensional array is very easy. Instead of one bracket, you will use two e.g. int[][] is a two-dimensional integer array. You can define a 2D array in Java as follows :
🌐
Coderanch
coderanch.com › t › 381555 › java › fill-array-file
Trying to fill a 2D array from a file (Java in General forum at Coderanch)
November 30, 2006 - //Fill the Array with maze values for(int i = 0; i < maze.length; i++) { for(int x = 0; x < maze[0].length; x++) { maze[i][x] = Scanner.next(); } } However, maze is of type char, and .next() can only be used on strings of course. I also get the classic "static context error" with this code. Not sure how to get around this. THanks. ... Worked out the static context error, but not sure how to go about match up the char array with a string data type still.