二维数组初始化器,后跟方括号

时间:2021-04-08 21:42:09

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?

为什么它是可计算的,我怎么能理解这样的声明呢?

6 个解决方案

#1


103  

What you are doing here is:

你在这里做的是:

  1. Declare a new variable int[] it (which is a one-dimensional array)
  2. 声明一个新的变量int[]它(它是一个一维数组)
  3. Assign its value from the first element [0]
  4. 从第一个元素[0]分配它的值
  5. of the two-dimensional array new int[][]
  6. 二维数组新int[][]
  7. which is initialized to be {{1}}
  8. 初始化为{{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.

你创建了一个二维数组你初始化它包含一个包含1的数组同时你取外部数组的第一个元素(一个包含1的一维数组)并把它赋给你的变量。

#2


29  

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

#3


9  

The new int[][]{{1}} allocates a new 1x1 2D array. The (only) element is set to 1.

新的int[][]{{1}}分配一个新的1x1 2D数组。元素(唯一)被设置为1。

The [0] returns a reference to the first (and the only) row, which is of type int[].

[0]返回对第一行(以及惟一一行)的引用,该行类型为int[]。

The int[] it = ... declares a variable and initializes it with the above reference.

int[] it =…声明一个变量并使用上面的引用初始化它。

The end result is equivalent to int[] it = new int[]{1}.

最终结果等于int[]它= new int[]{1}。

#4


6  

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.

从本质上说,您正在创建一个匿名的二维数组(new int[][][]{{1}}),然后使用这个二维数组的第一个元素({1}),并将其赋值给变量。

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.

新int[][]{ { 1 } }:这将创建一个二维数组只有一个元素:一个数组,其中包含一个数组,其中包含一个int(数量1)。因为它不是分配给一个变量在任何地方和不会访问过去的这句话,我们称之为“匿名”,它有最小的可能范围。

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

[0]:它访问我们上面创建的匿名二维数组的第一个元素。第一个元素是一个包含1的一维数组(即。{ 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.

作为补充说明,完全没有理由这样做,而且它看起来只是一个非常有趣的Java难题。

#5


6  

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

#6


5  

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

#1


103  

What you are doing here is:

你在这里做的是:

  1. Declare a new variable int[] it (which is a one-dimensional array)
  2. 声明一个新的变量int[]它(它是一个一维数组)
  3. Assign its value from the first element [0]
  4. 从第一个元素[0]分配它的值
  5. of the two-dimensional array new int[][]
  6. 二维数组新int[][]
  7. which is initialized to be {{1}}
  8. 初始化为{{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.

你创建了一个二维数组你初始化它包含一个包含1的数组同时你取外部数组的第一个元素(一个包含1的一维数组)并把它赋给你的变量。

#2


29  

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

#3


9  

The new int[][]{{1}} allocates a new 1x1 2D array. The (only) element is set to 1.

新的int[][]{{1}}分配一个新的1x1 2D数组。元素(唯一)被设置为1。

The [0] returns a reference to the first (and the only) row, which is of type int[].

[0]返回对第一行(以及惟一一行)的引用,该行类型为int[]。

The int[] it = ... declares a variable and initializes it with the above reference.

int[] it =…声明一个变量并使用上面的引用初始化它。

The end result is equivalent to int[] it = new int[]{1}.

最终结果等于int[]它= new int[]{1}。

#4


6  

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.

从本质上说,您正在创建一个匿名的二维数组(new int[][][]{{1}}),然后使用这个二维数组的第一个元素({1}),并将其赋值给变量。

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.

新int[][]{ { 1 } }:这将创建一个二维数组只有一个元素:一个数组,其中包含一个数组,其中包含一个int(数量1)。因为它不是分配给一个变量在任何地方和不会访问过去的这句话,我们称之为“匿名”,它有最小的可能范围。

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

[0]:它访问我们上面创建的匿名二维数组的第一个元素。第一个元素是一个包含1的一维数组(即。{ 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.

作为补充说明,完全没有理由这样做,而且它看起来只是一个非常有趣的Java难题。

#5


6  

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

#6


5  

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