Fundamentals 7 min read

Java 2D Arrays from Scratch: Declarations, Creation, and Manipulation

This tutorial walks through Java 2D arrays, covering their definition, variable declaration syntax, creation with the new keyword, direct initialization, element access, and modification, illustrated with code examples and visual tables.

Lisa Notes
Lisa Notes
Lisa Notes
Java 2D Arrays from Scratch: Declarations, Creation, and Manipulation

Definition of a two‑dimensional array

A two‑dimensional (2D) array is an array whose elements are themselves arrays. It is commonly visualized as a table with rows and columns, where the first index selects a row and the second index selects a column.

Declaring a 2D array variable

The declaration syntax mirrors that of a one‑dimensional array, differing only by the number of bracket pairs. int[][] array; An alternative but equivalent form places the brackets after the variable name:

int array[][];

Creating a 2D array object

Using the new keyword requires specifying the size of each dimension. The sizes are given in square brackets after the element type. int[][] array = new int[52][7]; This creates space for 52 rows (e.g., weeks) and 7 columns per row (e.g., days). The first index corresponds to the row, the second to the column. Indexing starts at 0, so the element at the first row and first column is accessed as array[0][0].

Direct initialization of array elements

Both dimensions can be created and initialized simultaneously by placing values inside nested braces, separated by commas.

int[][] array = { {1, 11, 22, 33, 44}, {1, 110, 20, 30, 40, 50} };

The example defines a 2‑row × 5‑column array. When the inner brace lengths differ, the resulting 2D array is jagged (rows may have different column counts).

int[][] jagged = { {11, 22}, {33, 44, 55, 66, 77}, {10, 20, 30} };

Accessing 2D array elements

Elements are accessed with two index brackets: the first for the row, the second for the column. System.out.println(array[1][2]); In the example above, array[1][2] refers to the element in the second row, third column, which prints 30.

Modifying 2D array elements

After creation, individual elements can be reassigned. The following program reads two elements, adds them, stores the result in a third element, and prints the result.

public class ArrayElementModification {
    public static void main(String[] args) {
        int[][] array = { {11, 11, 22}, {33, 44}, {10, 20, 30, 40, 50} };
        // add element at row 1, column 1 (value 44) to element at row 2, column 2 (value 30)
        array[0][0] = array[1][1] + array[2][2];
        System.out.println(array[0][0]); // prints 74
    }
}

The program demonstrates that array[0][0] receives the sum of array[1][1] and array[2][2], then outputs the new value.

Javaarray manipulationarray initialization2D arrayarray accessarray declaration
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.