它似乎也不是构造函数,因为我们会像函数调用一样使用它(例如 new String[](...) ) .
那么它是什么样的语法,我们在Java中有更多这样的语法吗?
4 回答
1
数组可以通过多种方式创建,如下所示, and you are using the second one which is called the Array Initializer where you create the array while also initializing it.
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"}; , this statement is creating an array of String values with elements "a", "b" and "c" with the implicit size of the array being 3 由于它被初始化的3个元素 .
4 回答
数组可以通过多种方式创建,如下所示, and you are using the second one which is called the Array Initializer where you create the array while also initializing it.
在第二个语句中,我创建了一个整数数组,其元素为1,2和3,其中大小隐含为3 .
因此,在你的情况下
String[] str = new String[]{"a","b","c"};
, this statement is creating an array of String values with elements "a", "b" and "c" with the implicit size of the array being 3 由于它被初始化的3个元素 .此语法是10.6. Array Initializer作为15.10.1. Array Creation Expression的一部分的示例 .
是一个数组创建表达式和
是一个数组初始值设定项 .
由于数组创建表达式中没有维度表达式(即方括号内没有任何内容),因此必须有一个数组初始值设定项:
语法与array initializer有关:
基本上,您不仅可以创建数组,还可以在同一条指令中初始化其所有字段 .
它不是演员 .
注意代码:
是创建和初始化数组的单个命令,但也可以使用较不详细的版本:
它被称为Array Initializer,其唯一目的,顾名思义,是初始化数组 .