Java "new Type[]{...}" 数组文字初始化是什么类型的语法?
What type of syntax is the Java "new Type[]{...}" array literal initialization?
引起我注意但我无法向自己解释的是对这段众所周知的代码的思考:
String[] str = new String[]{"a","b","c"};
new String[]
是演员吗?如果是,为什么我们使用 new
而没有括号?我们将按以下方式进行转换:
float i = (float) 3;
它似乎也不是构造函数,因为那样我们就可以像函数调用一样使用它(例如 new String[](...)
)。
那么它是什么样的语法,我们在 Java 中有更多这样的语法吗?
它叫做 Array Initializer,顾名思义,它的唯一用途是初始化数组。
可以通过多种方式创建数组,如下所示,您正在使用第二种称为数组初始化程序的方法,您可以在其中创建数组并对其进行初始化。
int[] abc = new int[3]; // This means an array of integers with size 3 is created.
int[] def = new int[]{1,2,3}; // This means an array of integers with size 3 is created and also initialized with the values 1, 2 and 3.
在第二条语句中,我创建了一个整数数组,其中元素为 1、2 和 3,其中大小隐式为 3。
因此,在您的情况下 String[] str = new String[]{"a","b","c"};
,此语句正在创建一个包含元素 "a"、"b" 和 "c" 的字符串值数组数组的隐式大小为 3,因为它被初始化为 3 个元素。
语法与array initializer相关:
An array initializer may be specified in a field declaration (§8.3, §9.3) or local variable declaration (§14.4), or as part of an array creation expression (§15.10.1), to create an array and provide some initial values.
基本上,您不仅可以创建数组,还可以在同一指令中初始化其所有字段。
这不是演员。
注意代码:
String[] str = new String[]{"a","b","c"};
是创建和初始化数组的单个命令,但也可以使用更简洁的版本:
String[] str = {"a","b","c"};
此语法是 10.6. Array Initializer as part of an 15.10.1. Array Creation Expression.
的示例
An array initializer may be specified in a field declaration (§8.3, §9.3) or local variable declaration (§14.4), or as part of an array creation expression (§15.10.1), to create an array and provide some initial values.
new String[]
是一个数组创建表达式并且
{"a","b","c"}
是一个数组初始值设定项。
由于您的数组创建表达式中没有维度表达式(即方括号内没有任何内容),因此必须有一个数组初始值设定项:
If there are no dimension expressions, then there must be an array initializer.
A newly allocated array will be initialized with the values provided by the array initializer as described in §10.6.
引起我注意但我无法向自己解释的是对这段众所周知的代码的思考:
String[] str = new String[]{"a","b","c"};
new String[]
是演员吗?如果是,为什么我们使用 new
而没有括号?我们将按以下方式进行转换:
float i = (float) 3;
它似乎也不是构造函数,因为那样我们就可以像函数调用一样使用它(例如 new String[](...)
)。
那么它是什么样的语法,我们在 Java 中有更多这样的语法吗?
它叫做 Array Initializer,顾名思义,它的唯一用途是初始化数组。
可以通过多种方式创建数组,如下所示,您正在使用第二种称为数组初始化程序的方法,您可以在其中创建数组并对其进行初始化。
int[] abc = new int[3]; // This means an array of integers with size 3 is created.
int[] def = new int[]{1,2,3}; // This means an array of integers with size 3 is created and also initialized with the values 1, 2 and 3.
在第二条语句中,我创建了一个整数数组,其中元素为 1、2 和 3,其中大小隐式为 3。
因此,在您的情况下 String[] str = new String[]{"a","b","c"};
,此语句正在创建一个包含元素 "a"、"b" 和 "c" 的字符串值数组数组的隐式大小为 3,因为它被初始化为 3 个元素。
语法与array initializer相关:
An array initializer may be specified in a field declaration (§8.3, §9.3) or local variable declaration (§14.4), or as part of an array creation expression (§15.10.1), to create an array and provide some initial values.
基本上,您不仅可以创建数组,还可以在同一指令中初始化其所有字段。
这不是演员。
注意代码:
String[] str = new String[]{"a","b","c"};
是创建和初始化数组的单个命令,但也可以使用更简洁的版本:
String[] str = {"a","b","c"};
此语法是 10.6. Array Initializer as part of an 15.10.1. Array Creation Expression.
的示例An array initializer may be specified in a field declaration (§8.3, §9.3) or local variable declaration (§14.4), or as part of an array creation expression (§15.10.1), to create an array and provide some initial values.
new String[]
是一个数组创建表达式并且
{"a","b","c"}
是一个数组初始值设定项。
由于您的数组创建表达式中没有维度表达式(即方括号内没有任何内容),因此必须有一个数组初始值设定项:
If there are no dimension expressions, then there must be an array initializer. A newly allocated array will be initialized with the values provided by the array initializer as described in §10.6.