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

Arrays in Java

Jagged Arrays in Java

A Jagged Array is an array of arrays where each row can have a different number of columns. Unlike regular 2D arrays, where all rows have the same number of columns, a jagged array allows for variable-sized rows.

This is useful when storing data structures like students in different classes where each class has a different number of students.

Syntax:

dataType[][] arrayName = new dataType[rows][];
arrayName[rowIndex] = new dataType[columnSize]; // Assigning columns dynamically

Example:

Let's say we have multiple classes, but each class has a different number of students.

class MasterBackend {
    public static void main(String[] args) {
        // Jagged Array: Students in multiple classes with different sizes
        String[][] classStudents = new String[3][]; // 3 classes

        // Assigning different number of students to each class
        classStudents[0] = new String[]{"Ayush", "Rahul"};  // Class 1: 2 students
        classStudents[1] = new String[]{"Neha", "Priya", "Karan"};  // Class 2: 3 students
        classStudents[2] = new String[]{"Ravi"};  // Class 3: 1 student

        System.out.println("Students in multiple classes (Jagged Array):");
        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 (Jagged Array):
Class 1: Ayush Rahul
Class 2: Neha Priya Karan
Class 3: Ravi

Explanation:

  • String[][] classStudents = new String[3][]; → Creates a jagged array with 3 rows (classes).

  • Each row (class) is assigned a different number of students.

  • classStudents[0] = new String[]{"Ayush", "Rahul"}; → Class 1 has 2 students.

  • classStudents[1] = new String[]{"Neha", "Priya", "Karan"}; → Class 2 has 3 students.

  • classStudents[2] = new String[]{"Ravi"}; → Class 3 has 1 student.

Jagged 3D Array Example

If we extend this to a 3D jagged array, we can store multiple schools where each school has a different number of classes, and each class has a different number of students.

class MasterBackend {
    public static void main(String[] args) {
        // Jagged 3D Array: Schools → Classes → Students (with different sizes)
        String[][][] schoolStudents = new String[2][][]; // 2 Schools

        // School 1 has 2 classes, School 2 has 1 class
        schoolStudents[0] = new String[2][];
        schoolStudents[1] = new String[1][];

        // Assigning different numbers of students to each class
        schoolStudents[0][0] = new String[]{"Ayush", "Rahul"}; // School 1, Class 1
        schoolStudents[0][1] = new String[]{"Neha", "Priya", "Karan"}; // School 1, Class 2

        schoolStudents[1][0] = new String[]{"Ravi", "Anita"}; // School 2, Class 1

        System.out.println("Students in multiple schools and classes (Jagged 3D Array):");
        for (int i = 0; i < schoolStudents.length; i++) {
            System.out.println("School " + (i + 1) + ":");
            for (int j = 0; j < schoolStudents[i].length; j++) {
                System.out.print("  Class " + (j + 1) + ": ");
                for (int k = 0; k < schoolStudents[i][j].length; k++) {
                    System.out.print(schoolStudents[i][j][k] + " ");
                }
                System.out.println();
            }
        }
    }
}

Output:

Students in multiple schools and classes (Jagged 3D Array):
School 1:
  Class 1: Ayush Rahul
  Class 2: Neha Priya Karan
School 2:
  Class 1: Ravi Anita