Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-dimensional array references to multi-dimensional array

Why does this code compile?

int[] array = new int[][]{{1}}[0];

On the left side is one-dimensional array. On the right I thougth that three-dimensional, but it is not?

like image 602
Pawel Avatar asked Dec 24 '13 21:12

Pawel


1 Answers

The right side is a one dimensional array that is the first (0th) element of the two-dimensional array

new int[][]{{1}}

To show it out more clearly lets add parenthesis

int[] array = (new int[][]{{1}}) [0];// [0] is returning first row of 2D array
                          // which is 1D array so it can be assigned to `array` 
like image 103
Ted Hopp Avatar answered Sep 23 '22 20:09

Ted Hopp