Skip to content
C

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. What 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.

2. Why 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.

3. Real-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.

4. Syntax

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

5. Example 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

6. Key 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.