Sunday, 29 January 2017

JAGGED ARRAY IN JAVA

Till now we have seen few examples of an array. Did you all noticed that we always fixed the size of rows and columns for a 2D array i.e. for every row equal number of columns are there. What if for every row the number of columns changes then how to overcome with this kind of problem. Let us take an example –
int num =  {       
                             {1,2,4,5},
                             {7,8,9},
                             {1,2}
                   }
Then in the above example how to work with this kind of problem because whenever we assign or fetch the records we fetch on the basis of its length. But here in the above example every row has different number of columns. So to overcome with this kind of problem, the concept of Jagged array is used.

As we are taking static values so for the above array we can fetch the records easily. But what when we fetch the records while giving values at runtime. Because in runtime we have to give the row and column numbers which will be applicable for all row as same column number.

Example –
public class Array9 {
          public static void main(String[ ] args) {
                   int num[ ][ ] =   {       
                                                                   {1,2,3,4},
                                                                   {4,6},
                                                                   {7,8,9}
                                                          };
                   for(int i[ ] : num)
                   {
                             for(int j : i)
                             {
                                      System.out.print(j+" ");
                             }
                             System.out.println();
                   }
          }
}

Output –
1 2 3 4
4 6
7 8 9

To work with different number of columns for each row, it’s better to leave the position of column as blank and take input at runtime for it.

Example –
import java.util.Scanner;
public class JaggedArray {
          public static void main(String[ ] args) {
                   int a[ ][ ] = new int[3][ ];
                   // first row with 4 column
                   a[0]=new int[4];
                   // second row with 3 column
                   a[1]=new int[3];
                   // third row with 2 column
                   a[2]=new int[2];
                   Scanner sc = new Scanner(System.in);
                   for(int i=0;i<a.length;i++)
                   {
                             for(int j=0;j<a[i].length;j++)
                             {
                                      a[i][j]=sc.nextInt();
                             }
                   }                 
                   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 –
1
2
3
4
5
6
7
8
9
1 2 3 4
5 6 7

8 9

No comments:

Post a Comment