I have a problem understanding this piece of code:
int[] it = new int[][]{{1}}[0];
Why is it compileable, and how can I understand such a declaration?
I have a problem understanding this piece of code:
int[] it = new int[][]{{1}}[0];
Why is it compileable, and how can I understand such a declaration?
What you are doing here is:
int[] it
(which is a one-dimensional array)[0]
new int[][]
{{1}}
So you create a two-dimensional array which you initialize to contain an array which contains 1
and at the same time you take the first element of the outer array (which is a one-dimensional array containing 1
) and assign it to your variable.
int[] it = new int[][]{{1}}[0];
Let's break this one down into each stage, and what it means.
new int[][]
This is a new multidimensional array.
{{1}}
This is a multidimensional array literal. It makes an array that looks like this:
[0] = [1]
[1] = []
[2] = []
...
[N] = []
So take note, each element inside this array is itself an array. Then you've specified that your variable it
is equal to the first array in the multidimensional array, so it equates directly to:
int[] it = new int[] {1};
The new int[][]{{1}}
allocates a new 1x1 2D array. The (only) element is set to 1.
The [0]
returns a reference to the first (and the only) row, which is of type int[]
.
The int[] it = ...
declares a variable and initializes it with the above reference.
The end result is equivalent to int[] it = new int[]{1}
.
Essentially, you are creating an anonymous two-dimensional array (new int[][]{{1}}
) and then taking the first element of that two-dimensional array ({1}
) and assigning it to the variable it
.
Let's go step-by-step, because this actually is kind of confusing.
new int[][]{{1}}
: This creates a two-dimensional array which has only one element: an array, which contains one array, which contains one int
(the number 1). Because it's not assigned to a variable anywhere and won't be accessible past this statement, we call it "anonymous", and it has the smallest scope possible.
[0]
: This accesses the first element of the anonymous two-dimensional array we created above. The first element is a one-dimensional array containing 1 (i.e., {1}
).
int[] it =
: Finally, here we take that retrieved one-dimensional array and store it in our variable.
As a side note, there's absolutely no reason to do it this way, and it seems like just a very interesting Java puzzle.
I will try to break it down
new int[][] // an array of arrays (or 2-dimensional array)
{1} // an array instance with a single element: 1
{{1}} // an array instance with a single element: above array instance
[0] // the first element of an array
So it is roughly equivalent to the following code:
int[] inner = new int[1]; // an empty array with length 1
inner[0] = 1;
int[][] outer = new int[1][];
outer[0] = inner;
int[] it = outer[0];
int[] it = new int[][]{{1}}[0];
The integer array it
gets assigned the following:
new int[][] // A new 2D array is created
{{1}} // This is an initializer. The first array in the first array gets an array of 1 item: '1'
[0] // Take the first array from the 2D array