Two dimensional array initializer followed by square brackets

JavaArraysCompilation

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

What you are doing here is:

  1. Declaring a new variable int[] it (which is a one-dimensional array)
  2. Assigning its value from the first element [0]
  3. of the two-dimensional array new int[][]
  4. which is initialized to be {{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.

Solution 2 - Java

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};



Solution 3 - Java

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}.

Solution 4 - Java

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.

Solution 5 - Java

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];

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2654250View Question on Stackoverflow
Solution 1 - JavaAdam AroldView Answer on Stackoverflow
Solution 2 - JavachristopherView Answer on Stackoverflow
Solution 3 - JavaNPEView Answer on Stackoverflow
Solution 4 - JavaasteriView Answer on Stackoverflow
Solution 5 - JavaMax FichtelmannView Answer on Stackoverflow
Solution 6 - JavaJeroen VannevelView Answer on Stackoverflow