简体   繁体   English

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

[英]What's the difference between two array declarations in Java?

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;" 我想知道两个括号的位置之间有什么区别,为什么当我将括号放在名称后面(例如数组1)时,为什么必须将其初始化为一组值或一个新的数组,但是在array2中,我可以简单地说“ int [] array2;” and then use it later...? 然后再使用...?

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方法是将括号放在数组名称之前。

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 : 即使在文档中,数组也没有“ 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[] ; 都声明两个类型为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. 就像Oscar Gomez所说的,现在的区别是第二个“方式”要求您指定变量的类型为array,而不仅仅是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

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: 有关更多信息,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM