When we write ‘int a[][] = new int [3][3]’ the array represents a matrix of 3 rows and 3 columns i.e. equal no of elements in each row. But, if we wish to have variable number of elements in each row, for example, 2 elements in first row, 3 elements in second row, we can go for Variable-size arrays which are also called ragged arrays. Variable sized arrays are possible in java because Java treats multidimensional array as ‘arrays of arrays’.
In java the following syntax is used to declare variable-size arrays.
int x[][]= new int[3][];
x[0] = new int [2];
x[1] = new int [3];
It is pretty evident that the first row of the matrix can only have 2 elements and the second row can only have 3 elements.
0 comments:
Post a Comment