+ 3

Elements in multidimensional arrays

Can a multidimensional array be designed like this: int[][] arr = {{1,2,3},4,{2,3},5,6}; That is, can we insert primitive or reference data values (of the same data type as that of array) inside the first array in a multidimensional array?

1st Dec 2018, 11:21 AM
Mrigankashekhar Shandilya
Mrigankashekhar Shandilya - avatar
1 Respuesta
+ 13
Mrigankashekhar Shandilya ● U are making a 2d multidimensional array here , so this array arr is 2d array having elements as 1d array ● but in example given in question , U have taken some elements of 2d array arr as integers also [but they can only be 1d array] so it will give error . int[][] arr = {{1,2,3},4,{2,3},5,6} //here {4,5,6} can be a element of arr as a 1d array , but without bracket they will be taken as int type ,not int[] type ● Arrays in java are mutable , U can try changing values of primitive data types of same type [I didn't tried for reference type yet , will be fun☺] ● NOTE : U can't resize an array in java , U can only change values of elements .
1st Dec 2018, 11:32 AM
Gaurav Agrawal
Gaurav Agrawal - avatar