Java中两个数组声明之间的区别是什么?

时间:2021-04-29 22:26:43

In my book they've been switching the way the declare arrays between the following two methods:

在我的书中,他们一直在以下两种方法之间切换声明数组的方式:

    int array1[] = {1, 2, 3};
    int[] array2 = {1, 2, 3};

I was wondering what the difference was between the positioning of the two brackets, and why is it that when I put the brackets after the name (such as in array 1), why I must either initialize it to a set of values or a new array, but in array2, I can simply say "int[] array2;" and then use it later...?

我想知道两个括号的位置之间有什么区别,为什么当我把括号放在名字后面(例如在数组1中)时,为什么我必须将它初始化为一组值或一个新的数组,但在array2中,我可以简单地说“int [] array2;”然后再用它......?

3 个解决方案

#1


7  

They are identical except that as you mention you have to initialize it if you put the brackets after the name. One advantage of declaring them before the name is multiple array initialization like this:

它们是相同的,除非你提到你必须初始化它,如果你把括号放在名称后面。在名称之前声明它们的一个优点是多个数组初始化,如下所示:

int [] myArray1, myArray2;

int myArray1[], myArray2[];

The Java way according to the documentation is to put the brackets before the array name.

根据文档的Java方法是将括号放在数组名称之前。

#2


1  

There is no difference between them as they both declare a variable of type "array of ints". There is no "Java way" (but a preferred way), even if the documentation, arrays are declared with brackets before the variable name :

它们之间没有区别,因为它们都声明了一个“int of int”类型的变量。没有“Java方式”(但首选方式),即使文档,数组在变量名称之前用括号声明:

int[] array1;

Note : Pay attention that "declaration" is not "initialisation" (or instanciation)

注意:注意“声明”不是“初始化”(或实例化)

int[] array1;   // declares an array of int named "array1"
                // at this point, "array1" is NOT an array, but null
                // Thus we "declare" a variable that will hold some 
                // data of type int[].

array1 = new int[] {1, 2, 3};   // legacy initialization; set an array of int
                                // with 3 values to "array1". Thus, we "initialize"
                                // the variable with some data of type int[]

Therefore,

因此,

int [] array1;
int array2[];

both declares two variables of type int[]; however, they are just declarations of data types, and not arrays yet. And like Oscar Gomez said, the difference now is that the second "way" requires you to specify that the variable is of type array, and not only an int.

都声明了两个int []类型的变量;但是,它们只是数据类型的声明,而不是数组。就像Oscar Gomez所说,现在的区别在于第二种“方式”要求你指定变量是数组类型,而不仅仅是int。

int i[], j;   // i is a data type array of int, while j is only an int
int [] k, l;  // both k and l are of the same type: array of int

#3


0  

An array is created by an array creation expression or an array initializer. The former applies to array2 (although in this case, an array initializer was provided), whereas the latter applies to array1.

数组由数组创建表达式或数组初始值设定项创建。前者适用于array2(虽然在这种情况下,提供了数组初始值设定项),而后者适用于array1。

For more information, see:

有关更多信息,请参阅:

#1


7  

They are identical except that as you mention you have to initialize it if you put the brackets after the name. One advantage of declaring them before the name is multiple array initialization like this:

它们是相同的,除非你提到你必须初始化它,如果你把括号放在名称后面。在名称之前声明它们的一个优点是多个数组初始化,如下所示:

int [] myArray1, myArray2;

int myArray1[], myArray2[];

The Java way according to the documentation is to put the brackets before the array name.

根据文档的Java方法是将括号放在数组名称之前。

#2


1  

There is no difference between them as they both declare a variable of type "array of ints". There is no "Java way" (but a preferred way), even if the documentation, arrays are declared with brackets before the variable name :

它们之间没有区别,因为它们都声明了一个“int of int”类型的变量。没有“Java方式”(但首选方式),即使文档,数组在变量名称之前用括号声明:

int[] array1;

Note : Pay attention that "declaration" is not "initialisation" (or instanciation)

注意:注意“声明”不是“初始化”(或实例化)

int[] array1;   // declares an array of int named "array1"
                // at this point, "array1" is NOT an array, but null
                // Thus we "declare" a variable that will hold some 
                // data of type int[].

array1 = new int[] {1, 2, 3};   // legacy initialization; set an array of int
                                // with 3 values to "array1". Thus, we "initialize"
                                // the variable with some data of type int[]

Therefore,

因此,

int [] array1;
int array2[];

both declares two variables of type int[]; however, they are just declarations of data types, and not arrays yet. And like Oscar Gomez said, the difference now is that the second "way" requires you to specify that the variable is of type array, and not only an int.

都声明了两个int []类型的变量;但是,它们只是数据类型的声明,而不是数组。就像Oscar Gomez所说,现在的区别在于第二种“方式”要求你指定变量是数组类型,而不仅仅是int。

int i[], j;   // i is a data type array of int, while j is only an int
int [] k, l;  // both k and l are of the same type: array of int

#3


0  

An array is created by an array creation expression or an array initializer. The former applies to array2 (although in this case, an array initializer was provided), whereas the latter applies to array1.

数组由数组创建表达式或数组初始值设定项创建。前者适用于array2(虽然在这种情况下,提供了数组初始值设定项),而后者适用于array1。

For more information, see:

有关更多信息,请参阅: