MULTI DIMENSIONAL ARRAY

Multi Dimensional Array


How to declare an array:

    int i[ ] [ ]=new int[3][2];// here it shows structure of two dimensional array. Two dimensional means the array element can contain another Array as values. Something like matrix format i.e. in format of rows and columns. Here [3][2] means its contain total 3 rows and each row contain 2 columns.

      javapadho
      initialize two Dimensional Array

How to populate two dimensional array:


    If we want to populate two dimensional array we should use the index in matrix format i.e rows and columns. [3][2] here 3 represents number of rows and 2 represents number of columns. Means for each row there are 2 columns.

    Example 1:

      javapadho
      populate two dimensional array

    Example 2:

    Two dimensional array with anonymous arrays.


    String s1[ ][ ]={{“a”,”b”},{“1”,”2”},{“p”,”q”}};

      javapadho
      Two dimensional array with anonymous arrays

How to iterate the two dimensional array:


    Example 1:

     
                    
                 public class ArrayDemo
                 {
                    public static void main(String[] args) 
                    {
                       String s1[ ][ ] = {{“java”,”j2ee”},{“mysql”,”oracle”},{“tomcat”,”jboss”}};
                       for ( int i=0;i< s1.length;i++)
                       {
                          String sub[ ]=s1[i];
                          for ( int j=0;j< sub.length;i++)
                          {
                             String s=sub[i];  or  String s = s1[i][j];
                             System.out.println(“Value at[“+i+”][“+j+”] is : ”+s);
                          }
                       }
                    }
                 }                 

    Output:

    Value at [0][0] is : java Value at[0][1] is : j2ee Value at [1][0] is : mysql Value at [1][1] is : oracle Value at [2][0] is : tomcat Value at [2][1] is : jboss

Limitations of Arrays:


  • First we can initialize the array size that is fixed. We can’t increase (or) decrease the array size.

  • Array can hold only one type of data type values. It can’t hold the multiple data type values.

  • Manipulation of data is very difficult.

  • Customization is not possible.

  • In order to overcome this draw back,sun people came up with one new concept it’s named as “collections framework”.