Jagged Array in Java:
- It is an array of arrays, in which each element is an array in turn.
- A special feature of this array type is that it is a Multidimensional array whose different sizes each element can have.
Create & Initialize Jagged Array
- You only define the first dimension, which represents a number of rows in the array, when constructing an array of arrays.
- You can construct a jagged array of two dimensions, as follows:
- int myarr1[][] = new int[3][];
- Within the above statement a two-dimensional array of three rows is declared. When the array is declared, as shown below, you may describe it as a Jagged Array:
- Myarr1[1] = new int[2];
- myarray1[2] = new int[3];
- myarray1[3] = new int[4];
class Main
{
publicstaticvoid main(String[] args)
{
intmyarr[][] = newint[3][];
myarr[0] = newint[] { 1, 2, 3 };
myarr[1] = newint[] { 4, 5 };
myarr[2] = newint[] { 6, 7, 8, 9, 10 };
System.out.println("Two dimensional Jagged Array:");
for (inti = 0; i<myarr.length; i++) {
for (intj = 0; j<myarr[i].length; j++)
System.out.print(myarr[i][j] + " ");
System.out.println();
}
}
}