Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Arrays in Java

2-Dimensional (2D) Array in Java

A 2D array is a table or matrix where data is stored in rows and columns. It is useful when dealing with structured data like student lists in multiple classes.

Syntax:

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

or

dataType[][] arrayName = {
    {value1, value2},
    {value3, value4}
};

Example:

A list of students in multiple classes.

class MasterBackend {
    public static void main(String[] args) {
        // 2D Array: Students in multiple classes
        String[][] classStudents = {
            {"Ayush", "Rahul"},   // Class 1
            {"Neha", "Priya"}     // Class 2
        };

        System.out.println("Students in multiple classes:");
        for (int i = 0; i < classStudents.length; i++) {
            System.out.print("Class " + (i + 1) + ": ");
            for (int j = 0; j < classStudents[i].length; j++) {
                System.out.print(classStudents[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

Students in multiple classes:
Class 1: Ayush Rahul
Class 2: Neha Priya

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board