πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Jagged Array in Java  


Introduction
  • A Jagged Array in Java is a type of 2D array where the number of columns in each row can be different.
  • It is also called an array of arrays.
  • Unlike a matrix (which has equal columns in each row), a jagged array allows rows of variable length.
  • For example :
    • int[][] jaggedArr = {
          {10, 20, 30},
          {40, 50},
          {60, 70, 80, 90}
      };
    • Jagged Array in Java

Working with Jagged Arrays:

  • To work with jagged arrays in Java, we need to follow these steps:
    1. Declare an array - Define the reference of the array with a specific data type.
    2. Create an array - Allocate memory for the array using the new keyword.
    3. Initialize an array - Assign values to the elements of the array.
    4. Retrieve elements of an array - Access array elements using their row and column index.
  • We will go through each step with examples below.
1. Declare a Jagged Array
  • Declaration defines a variable that can hold multiple arrays of the same data type.
  • At this stage, memory is not allocated; only the reference is declared.
  • How to Declare a Jagged Array (Syntax):
    • Syntax:
      dataType[][] arrayName;
    • Example:
      int[][] jaggedArr;
2. Create a Jagged Array
  • Array creation is the process of allocating memory for the array after it has been declared.
  • In Java, this is done using the new keyword.
  • Unlike a matrix array, each row in a jagged array can have a different number of columns.
  • How to Create a Jagged Array:
    • Syntax:
      arrayName = new dataType[numberOfRows][];
      arrayName[rowIndex] = new dataType[numberOfColumns];
    • Example:
      int[][] jaggedArr = new int[3][];  // 3 rows
      jaggedArr[0] = new int[3];  // first row has 3 columns
      jaggedArr[1] = new int[2];  // second row has 2 columns
      jaggedArr[2] = new int[4];  // third row has 4 columns
3. Initialize a Jagged Array
  • Array initialization is the process of assigning values to the elements of a jagged array.
  • After a jagged array is created, its elements have default values (e.g., 0 for int, 0.0 for float, null for objects).
  • How to Initialize a Jagged Array:
    • Syntax:
      arrayName[rowIndex][columnIndex] = value;
    • Example:
      int[][] jaggedArr = new int[3][];
      jaggedArr[0] = new int[3];  // first row has 3 columns
      jaggedArr[1] = new int[2];  // second row has 2 columns
      jaggedArr[2] = new int[4];  // third row has 4 columns
      
      // Assigning values
      jaggedArr[0][0] = 10;
      jaggedArr[0][1] = 20;
      jaggedArr[0][2] = 30;
      
      jaggedArr[1][0] = 40;
      jaggedArr[1][1] = 50;
      
      jaggedArr[2][0] = 60;
      jaggedArr[2][1] = 70;
      jaggedArr[2][2] = 80;
      jaggedArr[2][3] = 90;
4. Retrieve Elements of a Jagged Array
  • Retrieving elements from a Jagged Array means accessing the values stored in the array using their row and column indices.
  • Jagged array elements in Java are zero-indexed, so the first element is at index [0][0] and each row can have a different last column index [rowIndex][columns-1].
  • Different Ways to Retrieve Elements of a Jagged Array
    1. Using Row and Column Index:
      • int[][] jaggedArr = {
            {10, 20, 30},
            {40, 50},
            {60, 70, 80, 90}
        };
        
        System.out.println(jaggedArr[0][1]);  // Output: 20
        System.out.println(jaggedArr[2][3]);  // Output: 90
    2. Using Nested For Loop:
      • for(int i = 0; i < jaggedArr.length; i++)
        {
            for(int j = 0; j < jaggedArr[i].length; j++)
            {
                System.out.println(jaggedArr[i][j]);
            }
        }
    3. Using Enhanced For Loop (For-Each):
      • for(int[] row : jaggedArr)
        {
            for(int num : row)
            {
                System.out.println(num);
            }
        }

Jagged Array Programs:-
  • Program 1:
    public class JaggedArray1
    {
        public static void main(String[] args)
        {
            // 1. Declare and create a jagged array with 3 rows
            int[][] jaggedArr = new int[3][];
            jaggedArr[0] = new int[3];  // first row has 3 columns
            jaggedArr[1] = new int[2];  // second row has 2 columns
            jaggedArr[2] = new int[4];  // third row has 4 columns
    
            // 2. Initialize array elements
            jaggedArr[0][0] = 10;
            jaggedArr[0][1] = 20;
            jaggedArr[0][2] = 30;
    
            jaggedArr[1][0] = 40;
            jaggedArr[1][1] = 50;
    
            jaggedArr[2][0] = 60;
            jaggedArr[2][1] = 70;
            jaggedArr[2][2] = 80;
            jaggedArr[2][3] = 90;
    
            // 3. Access and print elements using nested for loop (Way 1)
            System.out.println("Way 1:");
            for (int i = 0; i < jaggedArr.length; i++)
            {
                for (int j = 0; j < jaggedArr[i].length; j++)
                {
                    System.out.print(jaggedArr[i][j] + " ");
                }
                System.out.println();
            }
    
            // 4. Access and print elements using nested for-each loop (Way 2)
            System.out.println("Way 2:");
            for (int[] row : jaggedArr)
            {
                for (int num : row)
                {
                    System.out.print(num + " ");
                }
                System.out.println();
            }
        }
    }
  • Program 2:
    public class MainJaggedArray2
    {
        public static void main(String[] args)
        {
            // 1. Declare, create, and initialize a jagged array in a single step
            int[][] jaggedArr = {
                {10, 20},         // first row has 2 elements
                {30, 40, 50, 60}, // second row has 4 elements
                {70, 80, 90}      // third row has 3 elements
            };
    
            // 2. Access and print array elements using nested for-each loop
            System.out.println("Jagged Array Elements:");
            for (int[] row : jaggedArr)
            {
                for (int num : row)
                {
                    System.out.print(num + " ");
                }
                System.out.println(); // Move to next row
            }
        }
    }