Jagged-Array Using For Each Loop 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_for {
    public static void main(String args[])
    {
 
        //Jagged Array using En hanced For Loop in Java Programming
        int a[][]={
                {10,20,30,40},
                {10,20,30},
                {10,20,30,50}
        };
        for(int k[]:a) {
            for(int l:k)
            {
                System.out.print(" "+l);
            }
            System.out.println("");
        }
 
    }
}
 

Output

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

Basic Programs