The best way is probably to just declare and assign all values at once. As shown here. Java will automatically figure out what size the array is and assign the values to like this.

int contents[][] = { {1, 2} , { 4, 5} };

Alternatively if you need to declare the array first, remember that each contents[0][0] points to a single integer value not an array of two. So to get the same assignment as above you would write:

contents[0][0] = 1;
contents[0][1] = 2;
contents[1][0] = 4;
contents[1][1] = 5;

Finally I should note that 2 by 2 array is index from 0 to 1 not 0 to 2.

Hope that helps.

Answer from lotu on Stack Overflow
🌐
Devcubicle
devcubicle.com › assign-values-two-dimensional-array-java
Assign values to two dimensional array in Java - DevCubicle By Cloud Tech
December 29, 2019 - In this post, we will look at how to assign values to two dimensional array in Java. We can assign value at declaration time or using index.
🌐
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
Now there are two ways to initialize a two-dimensional array in Java, either by using an array literal at the time of creation or by using nested for loop and going through each element.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-1-2D-arrays-Day2.html
8.1.5. Set Value(s) in a 2D Array (Day 2) — CSAwesome v1
Add another row of data to the arrays by changing the size of the arrays and adding in the assignment statements for the cells in those rows. ... 8-1-9: Which of the following sets the value for the 3rd row and 2nd column of a 2D array called nums?
Top answer
1 of 13
889

Try the following:

int[][] multi = new int[5][10];

... which is a short hand for something like this:

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:

int[][] multi = new int[][] {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

... or, more succinctly,

int[][] multi = {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
2 of 13
86

We can declare a two dimensional array and directly store elements at the time of its declaration as:

int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};

Here int represents integer type elements stored into the array and the array name is 'marks'. int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type.

Coming back to our statement written above: each row of elements should be written inside the curly braces. The rows and the elements in each row should be separated by a commas.

Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. These blocks can be individually referred ta as:

marks[0][0]  marks[0][1]  marks[0][2]  marks[0][3]  marks[0][4]
marks[1][0]  marks[1][1]  marks[1][2]  marks[1][3]  marks[1][4]
marks[2][0]  marks[2][1]  marks[2][2]  marks[2][3]  marks[2][4]


NOTE:
If you want to store n elements then the array index starts from zero and ends at n-1. Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator.

int marks[][];           // declare marks array
marks = new int[3][5];   // allocate memory for storing 15 elements

By combining the above two we can write:

int marks[][] = new int[3][5];
🌐
CodingTechRoom
codingtechroom.com › question › assign-values-to-2d-array
How to Explicitly Assign Values to a 2D Array in Programming? - CodingTechRoom
Assigning values to a two-dimensional (2D) array involves explicitly specifying values at designated index locations within the array. A 2D array can be visualized as a table or grid containing rows and columns.
Find elsewhere
🌐
Vertex Academy
vertex-academy.com › tutorials › en › two-dimensional-arrays-java
Two-Dimensional Arrays in Java • Vertex Academy
March 28, 2018 - The second "for" loop checks each element of each column in each row. By inserting one "for" loop inside another, you can assign values to elements of a two-dimensional array. ... You can find more articles in our Java Tutorial for Beginners.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › 2d arrays in java
2D Arrays in Java | A Comprehensive Guide and Examples
September 29, 2023 - For taking user input, we took the help of a scanner class in Java. We created the object of this class called s1. We created this object to use different methods specified in a class scanner. Further, we used the nextInt() method in the scanner class to take input from the user at a particular location. Here, we used nested for loops to loop over rows and columns. The first nesting set takes input from the user, which is nothing but inserting values in a 2-dimensional array.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Software Testing Help
softwaretestinghelp.com › home › java › multidimensional arrays in java (2d and 3d arrays in java)
MultiDimensional Arrays In Java (2d and 3d Arrays In Java)
April 1, 2025 - The approaches used to initialize a 3d array are the same as the ones used for initializing Two-dimensional arrays. You can either initialize the array by assigning values to individual array elements or initialize the array during the declaration.
🌐
Medium
adilwadhwania.medium.com › two-dimensional-array-in-java-97c0bd408032
Two dimensional array in java. Understand what is two dimensional… | by Adil | Medium
May 9, 2023 - The picture shows an array with ... arrays. To assign, update or access the elements in 2D arrays you need to used the row and column numbers(index) as shown in the table....
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › 6-example-to-declare-two-dimensional-array-in-java.html
6 ways to declare and initialize a two-dimensional (2D) String and Integer Array in Java - Example Tutorial
June 28, 2025 - Some of the ways described here also apply how you declare a one-dimensional array, like changing the place of square brackets and how it impacts any other variables declared in the same line. This is actually a tricky concept to remember and you will always find some questions on this topic on Java certifications like OCAJP and OCPJP.
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
Most commonly used type is the two-dimensional (2D) array. ... public class Geeks { public static void main(String[] args) { // Declaring a 2D array int[][] arr; // Initializing row and column sizes arr = new int[1][3]; // Assigning values arr[0][0] = 3; arr[0][1] = 5; arr[0][2] = 7; // Displaying values System.out.println("arr[0][0] = " + arr[0][0]); System.out.println("arr[0][1] = " + arr[0][1]); System.out.println("arr[0][2] = " + arr[0][2]); } }
Published: May 4, 2026
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
April 5, 2016 - In this situation, the remaining values are assigned to default values (0 in this case). In this programming, We can use the index position to access the two dimensional array elements.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › two-dimensional array in java
Two-Dimensional Array in Java | Syntax & Example Program
July 22, 2026 - In the above syntax, rowIndex represents the index of the row, columnIndex represents the index of the column, and value represents the element you want to assign or retrieve. Indices start from 0, so the valid index values for rows and columns range from 0 to (number of rows/columns - 1). Let us now create a two-dimensional integer array called matrix with three 3 rows and four columns:
🌐
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_venugopal › texts › notes-java › data › arrays › arrays-2D.html
Java: Arrays -- 2-dimensional
You can assign initial values to an array when in a manner very similar to one-dimensional arrays, but with an extra level of braces. The dimension sizes are computed by the compiler from the number of values. This would allocated a 3x3 board · int[][] board = new int[][] {{1,0,0},{0,1,0}...