Friday, April 12, 2019

dynamic 2d array in java

class cll
{
public static void main(String args[])
{

int a[][];
a=new int[2][];
for (int i=0;i<2;i++)
{
   a[i]=new int [i+2];
   for(int j=0;j<i+2;j++)
     {
            a[i][j]=i*j;
     }
}

System.out.println("matrix  ");
for (int i=0;i<2;i++)
{
 
   for(int j=0;j<i+2;j++)
     {
            System.out.print(a[i][j]+" ");
     }
     System.out.println(" ");
}

System.out.println("total row "+ a.length);
System.out.println("total column in row 0="+a[0].length);
System.out.println("total column in row 1="+a[1].length);

}
}




D:\>javac cll.java

D:\>java cll
matrix
0 0
0 1 2
total row 2
total column in row 0=2
total column in row 1=3

D:\>



No comments:

Post a Comment