0
0
Javaprogramming~15 mins

Two-dimensional arrays in Java

Choose your learning style8 modes available
menu_bookIntroduction

Two-dimensional arrays help you store data in a table-like form with rows and columns. This is useful when you want to organize information in grids, like a chessboard or a calendar.

When you want to store a matrix of numbers, like in math or graphics.
When you need to represent a game board, like tic-tac-toe or chess.
When you want to keep track of data in rows and columns, like a spreadsheet.
When you want to store multiple lists of related data together.
When you need to perform operations on grids, like image processing.
regular_expressionSyntax
Java
public class TwoDimensionalArrayExample {
    public static void main(String[] args) {
        // Declare and create a 2D array with 3 rows and 4 columns
        int[][] matrix = new int[3][4];

        // Assign a value to a specific cell
        matrix[0][0] = 5;

        // Access a value from the array
        int value = matrix[0][0];
    }
}

The first bracket [] represents rows, and the second [] represents columns.

Array indices start at 0, so the first row is index 0 and the first column is index 0.

emoji_objectsExamples
line_end_arrow_notchThis creates an empty 2D array with no rows and no columns.
Java
int[][] emptyArray = new int[0][0];
line_end_arrow_notchThis creates a 2D array with one row and one column containing the value 42.
Java
int[][] singleElementArray = {{42}};
line_end_arrow_notchThis creates a jagged 2D array where each row can have a different number of columns.
Java
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[3];
line_end_arrow_notchThis creates a 3x3 2D array with predefined values.
Java
int[][] predefinedArray = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
code_blocksSample Program

This program creates a 2D array with 2 rows and 3 columns. It first prints the array with default values (which are zeros). Then it fills the array with multiplication results of (row + 1) and (col + 1) and prints the updated array.

Java
public class TwoDimensionalArrayDemo {
    public static void main(String[] args) {
        // Create a 2D array with 2 rows and 3 columns
        int[][] table = new int[2][3];

        // Print the array before assigning values
        System.out.println("Before assigning values:");
        print2DArray(table);

        // Assign values to each cell
        for (int row = 0; row < table.length; row++) {
            for (int col = 0; col < table[row].length; col++) {
                table[row][col] = (row + 1) * (col + 1);
            }
        }

        // Print the array after assigning values
        System.out.println("After assigning values:");
        print2DArray(table);
    }

    // Helper method to print a 2D array
    public static void print2DArray(int[][] array) {
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length; col++) {
                System.out.print(array[row][col] + " ");
            }
            System.out.println();
        }
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Accessing or assigning an element uses array[row][column] syntax.

line_end_arrow_notch

Time complexity to access or assign one element is O(1).

line_end_arrow_notch

Be careful not to access indexes outside the array size to avoid errors.

list_alt_checkSummary

Two-dimensional arrays store data in rows and columns like a table.

Use array[row][column] to access or change values.

They are useful for grids, matrices, and tabular data.