[英]Java weird array behavior
Why is it that this works: 为什么这有效:
int[] array = {1, 2, 3};
but this doesn't: 但这不是:
int[] array;
array = {1, 2, 3};
If I have an array instance variable and I want to initialize it in my constructor surely I don't have to go 如果我有一个数组实例变量,我想在我的构造函数中初始化它肯定我不必去
array = new int[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
I feel like I'm missing something here? 我觉得我在这里错过了一些东西?
The literal syntax ie {}
can only be used when initializing during declaration. 字面语法即
{}
只能在声明期间初始化时使用。
Elsewhere you can do the following instead: 在其他地方,您可以执行以下操作:
int[] array;
array = new int[] {1, 2, 3};
The {...}
construct here is called an array initializer in Java. 这里的
{...}
构造在Java中称为数组初始化器。 It is a special shorthand that is only available in certain grammatical constructs: 这是一种特殊的速记,仅适用于某些语法结构:
JLS 10.6 Array Initializers
JLS 10.6阵列初始化器
An array initializer may be specified in a declaration , or as part of an array creation expression , creating an array and providing some initial values.
可以在声明中指定数组初始值设定项,也可以将其作为数组创建表达式的一部分 ,创建数组并提供一些初始值。 [...] An array initializer is written as a comma-separated list of expressions, enclosed by braces
"{"
and"}"
.[...]数组初始值设定项被写为逗号分隔的表达式列表,用大括号
"{"
和"}"
括起来。
As specified, you can only use this shorthand either in the declaration or in an array creation expression. 如指定的那样,您只能在声明或数组创建表达式中使用此简写。
int[] nums = { 1, 2, 3 }; // declaration
nums = new int[] { 4, 5, 6 }; // array creation
This is why the following does not compile: 这就是以下不编译的原因:
// DOES NOT COMPILE!!!
nums = { 1, 2, 3 };
// neither declaration nor array creation,
// array initializer syntax not available
Also note that: 另请注意:
Here's an example: 这是一个例子:
int[][] triangle = {
{ 1, },
{ 2, 3, },
{ 4, 5, 6, },
};
for (int[] row : triangle) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
The above prints: 以上打印:
1
2 3
4 5 6
java.util.Arrays
- has many array-related utility methods like equals
, toString
, etc java.util.Arrays
- 有许多与数组相关的实用程序方法,如equals
, toString
等 {}
是合成糖,允许您在初始化时使用值填充数组。
You can also do this: 你也可以这样做:
array = new int[]{1, 2, 3};
I'm not sure why it's necessary to specify the type on the righthand side in one version but not in the other. 我不确定为什么有必要在一个版本的右侧指定类型而不在另一个版本中指定类型。
Curly braces {} for initialization can only be used in array declaration statements. 用于初始化的Curly braces {}只能在数组声明语句中使用。 You can use:
您可以使用:
int[] array = new int[] {1,2,3}; // legal
but not: 但不是:
array = {1, 2, 3}; //illegal
http://www.janeg.ca/scjp/lang/arrays.html http://www.janeg.ca/scjp/lang/arrays.html
array is a reference. 数组是一个参考。 This means that when you write
array int[];
这意味着当你编写
array int[];
array will be equal to null. 数组将等于null。 if you want to use it, you need first to allocate the array , by
array = new int[3]
, and now you can fill it by. 如果你想使用它,首先需要通过
array = new int[3]
来分配数组,现在你可以填充它。
array[0] = 1;
array[1] = 2;
Now, java has two syntax sugar for filling array. 现在,java有两个语法糖用于填充数组。 you can write
arr int[] = {1,2,3}
or arr int[] = new int[]{1,2,3}
你可以写
arr int[] = {1,2,3}
或者arr int[] = new int[]{1,2,3}
Java will allocate and fill it behind the scene, but this is possible only on the deceleration. Java将在场景后面分配并填充它,但这只能在减速时实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.