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
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
2D arrays are declared by defining a data type followed by two sets of square brackets. ... In Java, when accessing the element from a 2D array using arr[first][second], the first index can be thought of as the desired row, and the second index ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
A 2D array represents data in rows and columns. It can be understood as an array of 1D arrays. ... A 2-D array can be seen as a table with 'x' rows and 'y' columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to ...
Published   May 4, 2026
Discussions

Syntax for creating a two-dimensional array in Java - Stack Overflow
Note that in your code only the first line of the 2D array is initialized to 0. Line 2 to 5 don't even exist. More on stackoverflow.com
🌐 stackoverflow.com
java - Create a two dimensional string array anArray[2][2] - Stack Overflow
This declares the size of your new 2D array. In Java (and most programming languages), your first value starts at 0, so the size of this array is actually 2 rows by 2 columns More on stackoverflow.com
🌐 stackoverflow.com
Finding neighbors in a 2D array

a while back i wrote a simple Game of Life program( probably not the best program out there). however, it uses the same 8 cell check around each cell. i used nested for loops to deal with it. the snippet below starts at line 121 of the program.

private void age() {
    generation++;
    //loop through all cells
    for ( int row = 0; row < rows; row++) {
        for ( int col = 0; col < cols; col++) {
            processCell( row, col);
        }
    }
    parent.updateGeneration( generation);
}

private void processCell( int row, int col) {
    int alive = 0;
    //start at top left cell, once cell away
    for ( int startRow = row == 0 ? 0 : row - 1; startRow < row + 2 && startRow < rows; startRow++) {
        for ( int startCol = col == 0 ? 0 : col - 1; startCol < col + 2 && startCol < cols; startCol++) {
            if ( cells[ startRow][ startCol].getGeneration() == generation) {
                if ( cells[ startRow][ startCol].wasAlive()) {
                    alive++;
                }
            } else if ( cells[ startRow][ startCol].isAlive()) {
                alive++;
            }
        }
    }
    if ( cells[ row][ col].isAlive()) {
        alive--;
    }
    if ( cells[ row][ col].isAlive() && (alive <= 1 || alive >= 4)) {
        changeCell( cells[ row][ col], false);
    } else if ( !cells[ row][ col].isAlive() && alive == 3) {
        changeCell( cells[ row][ col], true);
    }
} 

it might be helpful if u run the code in debug and see how it runs. if u need more help replay back to this and i try my best to help u, good luck.

More on reddit.com
🌐 r/javahelp
4
3
October 19, 2013
I want to store values of a 2D array into a 1D array in java.
You have some syntax errors in your code, but I'll concentrate on your question.You can just loop over your 2D array and assign values to a n*n length 1D array: int[] array1D = new int[n*n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i+n*j] = grid[i][j]; } } More on reddit.com
🌐 r/AskProgramming
13
15
September 7, 2020
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... A multidimensional array is an array that contains other arrays.
🌐
HWS Math
math.hws.edu › javanotes › c7 › s6.html
Javanotes 9, Section 7.6 -- Two-dimensional Arrays
This creates a 2D array of int that has 12 elements arranged in 3 rows and 4 columns. Although I haven't mentioned it, there are initializers for 2D arrays.
🌐
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.
🌐
CodeGym
codegym.cc › java blog › java arrays › 2d arrays in java
Java Matrix - 2D Arrays
April 8, 2025 - A 2D Array takes 2 dimensions, one for the row and one for the column. For example, if you specify an integer array int arr[4][4] then it means the matrix will have 4 rows and 4 columns.
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];
Find elsewhere
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › Array2dBasics › a2dDAS.html
10.3. Declaring 2D Arrays — AP CSA Java Review - Obsolete
To declare a 2D array, specify the type of elements that will be stored in the array, then ([][]) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
🌐
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 - This type of array is called a Jagged Array. ... import java.io.*; class GFG { public static void main(String[] args){ // declaring a 2D array with 2 rows int arr[][] = new int[2][]; // Jagged array with custom // columns for each row arr[0] ...
🌐
Scaler
scaler.com › home › topics › two dimensional array in java
Two Dimensional Array In Java with Examples - Scaler Topics
June 8, 2024 - In Java, this tabular representation of data is implemented using a two-dimensional array. A two-dimensional array (or 2D array in Java) is a linear data structure that is used to store data in tabular format.
🌐
Medium
medium.com › @AlexanderObregon › understanding-multi-dimensional-arrays-in-java-7ead0c3937dd
Understanding Multi-Dimensional Arrays in Java
February 22, 2025 - This tells Java that arrayName is a two-dimensional array that will store integers. The number of square brackets represents the dimensions of the array. If you had a 3D array, it would look like this: ... But for now, let’s focus on 2D arrays, as they are the most common type of multi-dimensional array.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › two-dimensional array in java
Two-Dimensional Array in Java | Syntax & Example Program
September 2, 2025 - Java 2D arrays organize data in a grid, ideal for tabular structures. Declared using square brackets, they simplify handling matrix-like data in Java. Two-Dimensional Array in Java - upGrad
🌐
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.
🌐
Medium
medium.com › @iamprem021 › a-simple-guide-to-2d-arrays-in-java-4f5f5e5e1a96
A Simple Guide to 2D Arrays in Java | by premprakash | Medium
May 24, 2024 - A 2D array in Java is an array of arrays. Think of it like a table with rows and columns, where each cell holds a value.
🌐
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 - A 2D array, in simple terms, is an array of arrays in Java. Picture it as a table where each cell can hold a value. Unlike one-dimensional arrays, which can be considered a single row, a 2D array has rows and columns.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
March 23, 2025 - Two Dimensional Array in Java means Array of Arrays. Java 2d Array or Two Dimensional Array, data stored in rows, columns & to access use index.
🌐
iO Flood
ioflood.com › blog › 2d-array-java
2D Array in Java: Configuring Two-Dimensional Arrays
February 27, 2024 - The elements of an array are stored ... of multidimensional array is the two-dimensional array, or 2D array, which is essentially a grid of values....
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit9-2DArray › a2dSummary.html
9.3. 2D Arrays Summary — CS Java
You can think of it as storing objects in rows and columns, but it actually uses an array of arrays to store the objects as shown below. In this chapter you learned how to declare 2d arrays, create them, and access array elements. Array elements are accessed using a row and column index. The first element in a 2d array is at row 0 and column 0. Figure 1: Java arrays of arrays¶ ·
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-1-2D-arrays-Day1.html
8.1.1. 2D Arrays (Day 1) — CSAwesome v1
Arrays in Java can store many items of the same type. You can even store items in two-dimensional (2D) arrays which are arrays that have both rows and columns. A row has horizontal elements. A column has vertical elements.