☕ Java

Zweidimensionale Arrays in Java 17: Ausführlicher Leitfaden

Two-Dimensional Arrays in Java

What is a Two-Dimensional Array?

Imagine a chessboard or an Excel table. Each cell has two coordinates: a row number and a column number. A two-dimensional array is the same kind of table, but it stores data (numbers, characters, strings).

Why is this needed? For example, to store:

  • A cinema hall layout (rows and seats)
  • A treasure island map
  • A table of bloggers' likes
  • Data about products in a warehouse

In simple terms: it is an array of arrays. First, we specify the row, and inside it — the columns.

How to Declare and Create a Two-Dimensional Array?

General syntax:

data_type[][] array_name = new data_type[number_of_rows][number_of_columns];

Example: let's create a 3×2 array (3 rows, 2 columns) to store grades:

int[][] grades = new int[3][2];
// Fill with values
grades[0][0] = 5;  // first row, first column
grades[0][1] = 4;  // first row, second column
grades[1][0] = 3;
grades[1][1] = 5;
grades[2][0] = 4;
grades[2][1] = 4;

You can also create and fill it immediately:

int[][] numbers = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Note: numbers[0][0] = 1, numbers[1][2] = 6, numbers[2][1] = 8.

Input and Output of a Two-Dimensional Array

Let's create a program that asks for dimensions and fills the array from the keyboard:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter number of rows: ");
        int rows = scanner.nextInt();
        System.out.print("Enter number of columns: ");
        int columns = scanner.nextInt();

        int[][] array = new int[rows][columns];

        // Fill the array
        System.out.println("Enter elements row by row:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                array[i][j] = scanner.nextInt();
            }
        }

        // Output the array as a table
        System.out.println("Your array:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println(); // new line
        }
    }
}

Example program output:

Enter number of rows: 2
Enter number of columns: 3
Enter elements row by row:
1 2 3
4 5 6
Your array:
1 2 3 
4 5 6

Analysis of a Typical Problem: "Cozy Cinema"

In this problem, you need to create a hall layout. We have rows and seats (columns). Let free seats be denoted by the character 'O', and occupied ones by 'X'.

import java.util.Scanner;

public class Cinema {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter number of rows: ");
        int rows = scanner.nextInt();
        System.out.print("Enter number of seats per row: ");
        int seats = scanner.nextInt();

        char[][] hall = new char[rows][seats];

        // Fill all seats as free
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < seats; j++) {
                hall[i][j] = 'O';
            }
        }

        // Output the hall layout
        System.out.println("Hall layout:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < seats; j++) {
                System.out.print(hall[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Example output:

Enter number of rows: 3
Enter number of seats per row: 4
Hall layout:
O O O O 
O O O O 
O O O O

Another Example: "Post Popularity"

We have bloggers (rows) and their posts (columns). We need to calculate the total number of likes:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Number of bloggers: ");
        int bloggers = scanner.nextInt();
        System.out.print("Number of posts per blogger: ");
        int posts = scanner.nextInt();

        int[][] likes = new int[bloggers][posts];

        // Input data
        System.out.println("Enter the number of likes for each post:");
        for (int i = 0; i < bloggers; i++) {
            System.out.println("Blogger " + (i+1) + ":");
            for (int j = 0; j < posts; j++) {
                likes[i][j] = scanner.nextInt();
            }
        }

        // Calculate total number of likes
        int total = 0;
        for (int i = 0; i < bloggers; i++) {
            for (int j = 0; j < posts; j++) {
                total += likes[i][j];
            }
        }

        System.out.println("Total likes: " + total);
    }
}

Example output:

Number of bloggers: 2
Number of posts per blogger: 3
Enter the number of likes for each post:
Blogger 1:
10 20 15
Blogger 2:
5 30 25
Total likes: 105

Common Beginner Mistakes

  • Array index out of bounds: if the array is 3×2, then row indices are from 0 to 2, column indices from 0 to 1. Accessing array[3][0] will cause an error.
  • Confusing rows and columns: the first index is the row, the second is the column. array[row][column].
  • Forgetting the nested loop: to traverse the entire array, you need two loops — one for rows, another for columns inside.
  • Incorrect declaration: int[][] arr = new int[][5]; — this is not allowed, you must specify the number