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 }
};
Answer from obataku on Stack Overflow
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];
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-to-declare-and-initialize-2-d-array-in-java
Different Ways To Declare And Initialize 2-D Array in Java - GeeksforGeeks
July 23, 2025 - import java.io.*; class GFG { public ... arr = new int[n][m]; // initializing the array elements using for loop for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = i + j; } } // printing the first three rows ...
Discussions

Initialization of 2D Array in Java? - Stack Overflow
I've a little confusion, when we declare & initialize a 2D array in java like given below More on stackoverflow.com
🌐 stackoverflow.com
FAQ: 2D Arrays: Java - Declaration, Initialization, and Assignment
This community-built FAQ covers the “Declaration, Initialization, and Assignment” exercise from the lesson “2D Arrays: Java”. Paths and Courses This exercise can be found in the following Codecademy content: Learn Java FAQs on the exercise Declaration, Initialization, and Assignment ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
May 21, 2021
How do I initialise the second dimension of a two- ...
r/javahelp: General subreddit for helping with **Java** code. More on reddit.com
🌐 r/javahelp
Two dimensional ArrayList, need help with constructor
There are probably better ways to solve your problem, but here is the answer to your question: List> list1 = new ArrayList>(); for (int i = 0; i < 10; i++) { List list2 = new ArrayList(); for (int j = 0; j < 10; j++) { list2.add(i * j); } list1.add(list2); } Integer value = list1.get(1).get(5); More on reddit.com
🌐 r/java
5
0
February 17, 2012
🌐
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.
🌐
Ruby-Doc.org
ruby-doc.org › home › two dimensional array in java – the ultimate guide with examples
Two Dimensional Array in Java - The Ultimate Guide with Examples - Ruby-Doc.org
April 10, 2026 - The syntax to declare a two dimensional array in Java is: ... Both are equivalent, but the first one is preferred as it clearly signifies that the variable is a 2D array. javaCopyEditint[][] numbers; // Declaring a 2D array of integers String[][] names; // Declaring a 2D array of Strings double[][] grades; // Declaring a 2D array of doubles · There are several ways to initialize a 2D array in Java.
🌐
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 - We have actually declared int[] only Another thing to remember about this code is that if multiple variables are declared in the same line they would be the type of int[] which is one dimensional, not two dimensional like in the following example prices is a 2D array but abc is just a one-dimensional int array. int[] prices[], abc; Again, this is a tricky array concept in Java and that's why you will often find questions on this topic on various Java certifications.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › Array2dBasics › a2dDAS.html
10.3. Declaring 2D Arrays — AP CSA Java Review - Obsolete
When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and false for type boolean. To explicitly put a value in an array you give the name of the array followed by the row index in brackets followed by the column index in brackets ...
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
Just like 1D arrays, 2D arrays are indexed starting at 0. //Given a 2d array called `arr` which stores `int` values ... In Java, initializer lists can be used to quickly give initial values to 2D arrays.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › initialize 2d array java
How to Initialize 2D Array in Java | Delft Stack
February 2, 2024 - The most common way to declare and initialize a 2-dimensional array in Java is using a shortcut syntax with an array initializer.
🌐
HappyCoders.eu
happycoders.eu › java › initialize-array-java
How to Initialize Arrays in Java
June 12, 2025 - As with one-dimensional arrays, all Java style guides prefer the first variant. A two-dimensional array can be initialized directly in the declaration – with new followed by the type and two pairs of square brackets:
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
To access an element of a two-dimensional array, you need two indexes: the first for the row, and the second for the column. Remember: Array indexes start at 0. That means row 0 is the first row, and column 0 is the first column.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › two-dimensional array in java
Two-Dimensional Array in Java | Syntax & Example Program
September 2, 2025 - In this example, we declare and initialize an array of integers called numbers with five elements. We then use a loop to access and print each array element. Next, we update the element's value at index 2 to 30. We access and print the updated element to verify the change. Lastly, we calculate the sum of all elements in the array using a loop and print the result. Mastering the two-dimensional array in Java is essential for handling structured data efficiently.
🌐
iO Flood
ioflood.com › blog › 2d-array-java
2D Array in Java: Configuring Two-Dimensional Arrays
February 27, 2024 - To create a 2D array in Java, you ... created a 2D array named array with 2 rows and 2 columns. By default, all elements of this array are initialized to zero....
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
March 23, 2025 - The Column size of an Array is 3. It means Employees array will only accept 3 integer values as columns. If we try to store more than 3, then it will throw an error. We can store less than 3. For Example, If we store 1 integer value, then the remaining 2 values will be initialized to the default value (Which is 0). We can initialize the Two Dimensional Array in multiple ways.
🌐
freeCodeCamp
freecodecamp.org › news › 2d-array-in-java-two-dimensional-and-nested-arrays
2D Array in Java – Two-Dimensional and Nested Arrays
August 10, 2022 - To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array.
🌐
Programiz
programiz.com › java-programming › multidimensional-array
Java Multidimensional Array (2d and 3d Array)
Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1. Let's take another example of the multidimensional array. This time we will be creating a 3-dimensional array. For example, ... Here, data is a 3d array that can hold a maximum of 24 (3*4*2) elements of type String. Here is how we can initialize a 2-dimensional array in Java.
🌐
sqlpey
sqlpey.com › java › java-2d-array-initialization
Java Two-Dimensional Arrays: Initialization and Usage - …
November 4, 2025 - A straightforward way to create a rectangular two-dimensional array where all rows have the same number of columns is by specifying both dimensions at once. Each element is automatically initialized to its default value, which is 0 for integers.
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit9-2DArray › a2dSummary.html
9.3. 2D Arrays Summary — CS Java
Initialize a 2d array of integers named nums so that it has 1,2,3 in the first row and 4,5,6 in the second row.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › java faq
FAQ: 2D Arrays: Java - Declaration, Initialization, and Assignment - Java FAQ - Codecademy Forums
May 21, 2021 - This community-built FAQ covers the “Declaration, Initialization, and Assignment” exercise from the lesson “2D Arrays: Java”. Paths and Courses This exercise can be found in the following Codecademy content: Learn …
🌐
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 - This method may be useful when the dimensions involved are smaller. As the array dimension grows, it is difficult to use this method of individually initializing the elements. The next method of initializing the 2d array in Java is by initializing the array at the time of declaration only.