Jagged Arryays in Java

Introduction to Multidimensional Arrays

Introduction to Jagged Arrays in Java

A jagged array in Java is an array of arrays, where each element of the array can be a different size. This is in contrast to a traditional multidimensional array, where all of the inner arrays must have the same size.

Jagged arrays can be useful for storing data that varies in size or is generated dynamically. For example, you could use a jagged array to store the results of a database query, where each row of the query may have a different number of columns

To create a jagged array, you first need to declare an array of references to arrays. Then, you can initialize each element of the outer array with a new array of the desired size. For example, the following code creates a jagged array with three rows, where the first row has three elements, the second row has two elements, and the third row has four elements:


  int[][] jaggedArray = new int[3][];
  jaggedArray[0] = new int[3];
  jaggedArray[1] = new int[2];
  jaggedArray[2] = new int[4];
 

To access an element of a jagged array, you use the same syntax as for a traditional multidimensional array. For example, the following code accesses the element at row 2, column 1 of the jagged array:


  int element = jaggedArray[2][1];
 

Jagged arrays can be more flexible than traditional multidimensional arrays, but they can also be more difficult to use. It is important to keep track of the size of each inner array, and to make sure that you do not access elements outside of the bounds of any array.

Here are some of the advantages of using jagged arrays in Java:
  • Flexibility: Jagged arrays can have different sizes for each row, making them more flexible than traditional multidimensional arrays.
  • Dynamic allocation: Jagged arrays allow you to allocate memory dynamically, meaning that you can specify the size of each sub-array at runtime, rather than at compile-time.
  • Efficiency: Jagged arrays can be more efficient than traditional multidimensional arrays for storing and accessing data that varies in size.

Overall, jagged arrays are a powerful tool that can be used to store and access data in a variety of ways. However, it is important to be aware of the advantages and disadvantages of using jagged arrays before deciding whether or not to use them in your code.

Exercises

Create a jagged array and intialize it using the following values for its rows and columns.
1 1 1 1 1
2 2
3 3 3 3
4 4
Display the values in the array using an enhanced for loop


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
      public static void main(String args[]) 
      {
       int jaggedArr[][] = new int[4][];
          
          jaggedArr[0] = new int[5];
          jaggedArr[1] = new int[2];
          jaggedArr[2] = new int[4];
          jaggedArr[3] = new int[2];
          
          //filling values
          for(int i=0;i<jaggedArr.length;i++)
          {
              for(int j=0;j<jaggedArr[i].length;j++)
              {
                  jaggedArr[i][j] = i+1;
              }
          }
          
          //printing
          for(int m[]:jaggedArr)
          {
              for(int k:m)
              {
                  System.out.print(k+" ");
              }
              System.out.println();
          }
      }
  }
  
  Output
  1 1 1 1 1 
  2 2 
  3 3 3 3 
  4 4    

Create a jagged array and intialize it using the following values for its rows and columns.
22 44 12 15 16
2 25 32 33
4 4 1 3
1 3 4
Find and display the sum of each row in the array


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
    public static void main(String args[]) 
    {
    	  int jaggedArr[][] = {{22,44,12,15,16},{2,25,32,33},{4,4,1,3},{1,3,4}};
          
          for(int i=0;i<jaggedArr.length;i++)
          {
              int sum=0;
              for(int j=0;j<jaggedArr[i].length;j++)
              {
                  sum+=jaggedArr[i][j];
              }
              
              System.out.println("The sum of row "+i+" is = "+sum);
          }
    }
}

Output
The sum of row 0 is = 109
The sum of row 1 is = 92
The sum of row 2 is = 12
The sum of row 3 is = 8
 

Create a Jagged Array by getting the number of rows and columns from the user and printing the data in the array to the user.


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
    public static void main(String args[]) 
    {
    	   Scanner sc = new Scanner(System.in);
           System.out.println("Enter the number of rows of jagged array :");
           int row = sc.nextInt();
           
           int jaggedArr[][] = new int[row][];
           
           for(int i=0;i<row;i++)
           {
               System.out.println("Enter the number of elements in row "+i+" : ");
               int col = sc.nextInt();
               
               jaggedArr[i] = new int[col];
               
               System.out.println("Enter the elements :");
               for(int j=0;j<col;j++)
               {
                   jaggedArr[i][j] = sc.nextInt();
               }
           }
           
           System.out.println("Displaying the jagged Array :");
           for(int m[]:jaggedArr)
           {
               for(int k:m)
               {
                   System.out.print(k + " ");
               }
               System.out.println();
           }
    }
}

Output
Enter the number of rows of jagged array :
3
Enter the number of elements in row 0 : 
3
Enter the elements :
1
1
1
Enter the number of elements in row 1 : 
2
Enter the elements :
1
1
Enter the number of elements in row 2 : 
1
Enter the elements :
4
Displaying the jagged Array :
1 1 1 
1 1 
4  

Code for creating a Jagged Array by getting the user input and finding the minimum and maximum number from each row


  package javalabdemo;
  import java.util.Arrays;
  import java.util.Scanner;
  public class javalabclass 
  {
  public static void main(String args[])
    {
    //creating a jagged array by getting the input from the user 
    //enter the number of rows for the array
    //sort each row of the jagged array in the ascending order
    //we will print the maximum and minimum number in each row 
    System.out.println("Enter the number of rows for the array");
    Scanner input = new Scanner(System.in);
    int numRows = input.nextInt();
    int a[][] = new int[numRows][];
    int rowlength=0;
    for(int i=0;i<numRows;i++)
    {
      System.out.println("Enter the size for the row");
      rowlength = input.nextInt();
      a[i]=new int[rowlength];
    }
    System.out.println("Enter the elements for the jagged array");
    for(int i=0;i<a.length;i++)
    {
      for(int j=0;j<a[i].length;j++)
      {
        a[i][j]=input.nextInt();
      }
    }
    //to print the data back to the user 
    for(int[] k:a)
    {	
      Arrays.sort(k);
      for(int m:k)
      {
        System.out.print(m);
      }
      System.out.println();
      System.out.println("The minimum number in the row is" +k[0]);
      System.out.println("The maximum number in the row is"+ k[k.length-1]);
    }
    
    }
  }