Java int[]数组和int数组[]的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/129178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:38:02  来源:igfitidea点击:

Difference between int[] array and int array[]

javaarrays

提问by mslot

I have recently been thinking about the difference between the two ways of defining an array:

最近一直在思考两种定义数组的方式的区别:

  1. int[] array
  2. int array[]
  1. int[] array
  2. int array[]

Is there a difference?

有区别吗?

采纳答案by skaffman

They are semantically identical. The int array[]syntax was only added to help C programmers get used to java.

它们在语义上是相同的。该int array[]语法只加入到帮助C程序员习惯的Java。

int[] arrayis much preferable, and less confusing.

int[] array更可取,并且不那么令人困惑。

回答by Zach Lute

In Java, these are simply different syntactic methods of saying the same thing.

在 Java 中,这些只是说同一件事的不同句法方法。

回答by basszero

They're the same. One is more readable (to some) than the other.

他们是一样的。一种比另一种更具可读性(对某些人而言)。

回答by Derek Park

They are completely equivalent. int [] arrayis the preferred style. int array[]is just provided as an equivalent, C-compatible style.

它们是完全等效的。 int [] array是首选风格。 int array[]只是作为等效的、与 C 兼容的样式提供。

回答by André

There is no difference, but Sun recommends putting it next to the type as explained here

没有区别,但 Sun 建议按照此处的说明将其放在类型旁边

回答by George Strother

The two commands are the same thing.

这两个命令是一样的。

You can use the syntax to declare multiple objects:

您可以使用语法来声明多个对象:

int[] arrayOne, arrayTwo; //both arrays

int arrayOne[], intOne; //one array one int 

see: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

见:http: //java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

回答by Adam Rosenfield

There is one slight difference, if you happen to declare more than one variable in the same declaration:

如果您碰巧在同一个声明中声明了多个变量,则有一个细微的区别:

int[] a, b;  // Both a and b are arrays of type int
int c[], d;  // WARNING: c is an array, but d is just a regular int

Note that this is bad coding style, although the compiler will almost certainly catch your error the moment you try to use d.

请注意,这是一种糟糕的编码风格,尽管编译器几乎肯定会在您尝试使用d.

回答by Luke Woodward

There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.

两者没有任何区别;两者都声明了一个ints数组。然而,前者是首选,因为它将类型信息全部保存在一个地方。后者只真正支持 C/C++ 程序员转向 Java。

回答by Yuval Adam

No difference.

没有不同。

Quoting from Sun:

引用Sun 的话

The []may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example: byte[] rowvector, colvector, matrix[];

This declaration is equivalent to: byte rowvector[], colvector[], matrix[][];

[]可能显示为在声明的开头所述类型的部分,或作为说明符用于特定变量,或两者的一部分,如在这个例子中:byte[] rowvector, colvector, matrix[];

此声明等效于: byte rowvector[], colvector[], matrix[][];

回答by TofuBeer

There is no difference.

没有区别。

I prefer the type[] nameformat at is is clear that the variable is an array (less looking around to find out what it is).

我更喜欢type[] name格式很清楚,变量是一个数组(较少四处寻找它是什么)。

EDIT:

编辑:

Oh wait there is a difference (I forgot because I never declare more than one variable at a time):

哦等等有区别(我忘记了,因为我一次从不声明多个变量):

int[] foo, bar; // both are arrays
int foo[], bar; // foo is an array, bar is an int.