Jagged-Array in Java


A jagged array is an array of arrays such that member arrays can be of different row sizes and column sizes. Jagged subarrays may also be null. For instance, the following code declares and populates a two dimensional int array whose first subarray is of four length, second subarray is of three length, and the last subarray is a fours length array:

  Example :
        int [ ] [ ] a = { { 10,20,30,40 } , { 10,20,30 } , { 10,20,30,50 } } ;

Source Code

public class jagged_Array {
    public static void main(String args[]) {
        //Jagged Array using For Loop in Java Programming
        int a[][] = {
                {10, 20, 30, 40},//4
                {10, 20, 30},//3
                {10, 20, 30, 50}//4
        };
 
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(" "+a[i][j]);
            }
            System.out.println("");
        }
    }
}
 

Output

 10 20 30 40
 10 20 30
 10 20 30 50
To download raw file Click Here

Basic Programs