Skip to content
CampusEduX

Arrays & Strings · Lesson 38 of 150

Two-Dimensional Array

A two-dimensional array stores data in the form of rows and columns, like a table or grid. It's essentially an array of arrays.

1 min read

01What is a Two-Dimensional Array?

A two-dimensional array stores data in the form of rows and columns, like a table or grid. It's essentially an array of arrays.

02Why is it used?

Some data naturally fits a grid structure — like a seating chart in a classroom, or a matrix used in calculations. A two-dimensional array represents this row-and-column structure directly.

03Real-Life Example

Think of a chessboard with 8 rows and 8 columns. Every square can be identified using its row number and column number together — exactly how elements in a two-dimensional array are accessed.

04Syntax

java
dataType[][] arrayName = new dataType[rows][columns];

05Example Program

java
public class TwoDArrayDemo { public static void main(String[] args) { int[][] grid = { {1, 2, 3}, {4, 5, 6} }; System.out.println("Element at row 1, column 2: " + grid[1][2]); } }

Output:

Element at row 1, column 2: 6

06Key Points to Remember

  • Access an element using two indices: array[row][column].
  • The number of rows and columns don't have to be equal.
  • Nested loops (row loop inside column loop, or vice versa) are the usual way to process every element of a 2D array.