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
888

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
where int[][] is just a type identifier, we cannot specify size. Moreover, in Java, we can initialize a 2D array that is not rectangular: More on stackoverflow.com
🌐 stackoverflow.com
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
Thanks for all the replies, I had to hand over my project before any of the replies ticked in. But i read them now so thanks guys. I just ended up making a "flattened array" of generics and just convert them. I used this method to find the index: public int finnIndeks(int x, int y){ return (x+hight*y); } And from my "get" method i just converted from non-generic to generic sort of: public E hent(int x, int y){ return (E) tabell[finnIndeks(x, y)]; } I guess this is not the best way to go about this problem. But it certainly was the simplest. It's an unchecked cast from object E so thats sort of bad. More on reddit.com
🌐 r/java
5
0
February 17, 2012
Multidimensional array list initialization
int ia[3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} }; This is how you're supposed to do it. I would change this up slightly: int ia[][] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} }; The compiler can count, so you don't have to. int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; This is that last link that u/neiltechnician linked to, that you don't need braces to initialize sub-aggregates. The elements of the initializer list match one to one to the sub-aggregates and their elements in the order listed in the source code, so once you've initialized the first sub-aggregate, which is a 4 element aggregate, the 5th initializer spills over into the next. I wouldn't necessarily suggest doing this, because it's not clear. It works because you can do something similar like with a template parameter pack that would expand into the above, and it would work. Flat packs are easier to work with than recursive hierarchies of aggregates. int ia[3][4] = {{ 0 }, { 4 }, { 8 }}; The rule is, if there is an initializer list, then any unspecified elements are default initialized. So if I wrote this: int abc[123] = {}; I just default initialized this whole array to a bunch of zeros, 123 int{} initializers, which is 0. It's a useful trick, mostly for 1D arrays. So here you have specified 4 sub-aggregates, each of which initializes their first elements, so the rest of each sub-aggregate gets initialized to zeros. It's actually kind of odd to initialize some members, but not others - usually you'll be explicitly initializing either everything to something, or implicitly initializing everything to their defaults. To initialize only the first member of each sub-aggregate but not the rest, usually that will come up in some very specific code, some very specific algorithm - like if you're computing the Fibonacci sequence and you initialize the first two members to 1, and 1... Nothing wrong with it, it's just not that common, this is just a kind of awareness comment. int ia[3][4] = {0, 3, 6, 9}; This is a combination of #2 and #3, you've flattened the aggregate list and only initialized some of the members. More on reddit.com
🌐 r/cpp_questions
2
1
March 6, 2023
🌐
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 › 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.
🌐
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.
🌐
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.
Find elsewhere
🌐
HappyCoders
happycoders.eu › home › java › how to initialize arrays in 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:
🌐
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.
🌐
Programiz
programiz.com › java-programming › multidimensional-array
Java Multidimensional Array (2d and 3d Array)
Remember, Java uses zero-based ... 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 ...
🌐
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.
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
April 5, 2016 - We can store less than 5. For Example, if we store 2 integer values, then the remaining 2 values will be initialized to the default value (Which is 0). Please refer to the Learn Java ...
🌐
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 - Here are the indexes in that array: 17 => Index 0 19 => Index 1 21 => Index 2 23 => Index 3 · 21 has an index of 2 so we can go on and add that to the second square bracket: oddNumbers[2][2]. When you print that to the console, you'll get 21 printed out. ... int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23} }; System.out.println(oddNumbers[2][2]); // 21 · You can loop through all the items in a two dimensional array by using a nested loop.
🌐
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....
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › two-dimensional array in java
Two-Dimensional Array in Java | Syntax & Example Program
September 2, 2025 - In the above program, we declare and initialize a 3x3 two-dimensional array called matrix. Then, we use nested loops to iterate over the array and print its elements row by row.
🌐
javaspring
javaspring.net › blog › java-two-dimensional-array-initialization
Java Two Dimensional Array Initialization: A Comprehensive Guide — javaspring.net
The general syntax of declaring ... and arrayName is the name of the array. Static initialization is used when you know the values of the array elements at the time of creation....
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Examples Java Videos Java ... in a table with rows and columns. To create a two-dimensional array, write each row inside its own curly braces:...
🌐
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.
🌐
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.