Java support jagged array. U can have different no of length for each column do don't need to have same no of column for all rows.
Example:-
package javaTest;
public class JaggedArray {
public static void main(String ar[])
{
int[][] a=new int[2][];
a[0]=new int[2];
a[1]=new int[4];
a[0][0]=1;
a[0][1]=12;
a[1][0]=10;
a[1][1]=3;
a[1][2]=12;
a[1][3]=13;
System.out.println("length-"+a.length);
for (int i = 0; i < a.length; i++) {
for (int k = 0; k < a[i].length; k++) {
System.out.print(a[i][k]+" , ");
}
System.out.println("");
}
}
}
Output:-
length-2
1 , 12 ,
10 , 3 , 12 , 13 ,
Example:-
package javaTest;
public class JaggedArray {
public static void main(String ar[])
{
int[][] a=new int[2][];
a[0]=new int[2];
a[1]=new int[4];
a[0][0]=1;
a[0][1]=12;
a[1][0]=10;
a[1][1]=3;
a[1][2]=12;
a[1][3]=13;
System.out.println("length-"+a.length);
for (int i = 0; i < a.length; i++) {
for (int k = 0; k < a[i].length; k++) {
System.out.print(a[i][k]+" , ");
}
System.out.println("");
}
}
}
Output:-
length-2
1 , 12 ,
10 , 3 , 12 , 13 ,
No comments:
Post a Comment