Sunday, 29 January 2017

MULTI-DIMENSIONAL ARRAY IN JAVA

Let us take an realtime example to understand why we basically use multi-dimensional array. To make some relationship among the related data we use multi-dimensional array. Suppose David has 3 wife then we will store the value of his wife into an array. Again suppose his wife has few children and we want to store the record of his children also, then again we have to store it into an array. Which means all the time we creates arrays of array.

Exampe –
public class Array10 {
          public static void main(String[] args) {
                   String Person[][][]={
                                                                             {
                                                                             {"David"},
                                                                             {"w1","w2","w3"},
                                                                             {"s1","s2"}
                                                                             },
                                                                             {
                                                                             {"Harry"},
                                                                             {"w1","w2","w3"},
                                                                             {"s1","s2","s3"}
                                                                             },
                                                                             {
                                                                             {"Wales"},
                                                                             {"w1"},
                                                                             {"s1","s2"}                              
                                                                             }
                                                                   };
                   for(String[][] a : Person)
                   {
                             for(String[] b : a)
                             {
                                      for(String c :b)
                                      {
                                                System.out.print(c+" ");
                                      }
                                      System.out.println();
                             }
                   }
          }
}

Output –
David
w1 w2 w3
s1 s2
Harry
w1 w2 w3
s1 s2 s3
Wales
w1

s1 s2

No comments:

Post a Comment