Print pyramid using jagged array implementation in java (static way or dynamic way)

Introducation to Jagged Array :

It is an array of arrays where each element is, in turn, an array. A special feature of this type of array is that it is a Multidimensional array whose each element can have different sizes.

For Example, a two-dimensional array in Java is an array of single dimension array. In the case of a two-dimensional array, each one-dimensional array will have different columns.

See this below code for better understand actual use of jagged array.

Example - 1 Print Pyramid Output Using Jagged Array (Static Way) :

package gsh.java.programs;
public class GSH {
    public static void main(String[] args) {
        char[][] jaggedArray = new char[5][];
        
        jaggedArray[0] = new char[1];
        jaggedArray[1] = new char[2];
        jaggedArray[2] = new char[3];
        jaggedArray[3] = new char[4];
        jaggedArray[4] = new char[5];

        jaggedArray[0][0] = '*';

        jaggedArray[1][0] = '*';
        jaggedArray[1][1] = '*';

        jaggedArray[2][0] = '*';
        jaggedArray[2][1] = '*';
        jaggedArray[2][2] = '*';

        jaggedArray[3][0] = '*';
        jaggedArray[3][1] = '*';
        jaggedArray[3][2] = '*';
        jaggedArray[3][3] = '*';

        jaggedArray[4][0] = '*';
        jaggedArray[4][1] = '*';
        jaggedArray[4][2] = '*';
        jaggedArray[4][3] = '*';
        jaggedArray[4][4] = '*';
        
        for (int i=0;i<jaggedArray.length;i++){
           for (int j=0;j<jaggedArray[i].length;j++){
                System.out.print(jaggedArray[i][j]+" ");
            }
            System.out.println("");
        }
   }
}

Output:

* 
* * 
* * * 
* * * * 
* * * * * 

Example - 2 Print Pyramid Output Using Jagged Array (Static Way) :

package gsh.java.programs;
public class GSH {
    public static void main(String[] args) {
	char[][] jaggedArray = {
                new char[] {'*'},
                new char[] {'*','*'},
                new char[] {'*','*','*'},
                new char[] {'*','*','*','*'},
                new char[] {'*','*','*','*','*'},
    	};
        
          for (int i=0;i<jaggedArray.length;i++){
            for (int j=0;j<jaggedArray[i].length;j++){
                System.out.print(jaggedArray[i][j]+" ");
            }
            System.out.println("");
        }
	}
}

Output:

* 
* * 
* * * 
* * * * 
* * * * * 

Example - 3 Print Pyramid Output Using Jagged Array (Dynamic Way) :

package gsh.java.programs;
import java.util.Scanner;
public class GSH {
    public static void main(String[] args) {
   Scanner sc =  new Scanner(System.in);
        System.out.println("Enter any number to print pyramid output using jagged array : ");
        int n = sc.nextInt();
        
        char[][] jaggedArray = new char[n][];
        for (int i=0;i<jaggedArray.length;i++){
            jaggedArray[i] = new char[i+1];
        }
        
        for (int i=0;i<jaggedArray.length;i++){
            for (int j=0;j<jaggedArray[i].length;j++){
                jaggedArray[i][j] = '*';
                System.out.print(jaggedArray[i][j]+" ");
            }
            System.out.println("");
        }
	}
}

Output:

* 
* * 
* * * 
* * * * 
* * * * *