Ad space (banner)
โ˜•Java Lessons
Lesson 24 / 68

Two-Dimensional Arrays (Tabular Data)

This lesson covers using a two-dimensional array in Java, so you can work with tabular data. It's for anyone searching "Java 2D array usage."

Writing int[][] grid represents a "grid-like table" with rows and columns โ€” called a two-dimensional array. Access it with grid[row][col].

The example declares data with 2 rows and 3 columns as {{1, 2, 3}, {4, 5, 6}}, and accesses an element using two stacked brackets, like grid[1][2]. Notice the enhanced for loop, for (int[] row : grid), pulling out each row at once.

A common early mistake is trying to display the array directly with println(). An array printed directly shows something like a memory address, so you need to use Arrays.toString() instead.

In real projects, this idea gets applied to things like a game board with coordinates, or spreadsheet-like data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)