Why java allows zero length index(in middle) array? -


javac allows below syntax,

int[][][] = new int[4][0][2]; 

which has 0 length index prevents access beyond.

1) there no way access third dimension. 0 length dimension last dimension(int[][][] = new int[4][2][0];) looks fine.

2) not possible write initialiser multi-dimensional array 0 length dimension unless dimension last( instance int[2][3][0]).

why java allows such syntax?

note: question has nothing int[0]

because nothing in multianewarray bytecode instruction prevents doing so.

there no better answer that... fact x, if x primitive, x[] class, x[][] class , on; , free choose "dimensions" of array.

note how declaring x[n][] , x[n][m] array differ: in first you'll declare anewarray of x[] whereas in second you'll declare multianewarray of x.

of course, in x[m][n][p], there no possibility ever have "third dimension" (p) if n 0, but... well, programmer knows he's doing, right?

just bizarreness of arrays in jvm... think nothing of except "it can happen" ;)


Comments