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

int[]数组 int[]数组

有区别吗?


当前回答

两者的意思是一样的。然而,这些变量的存在也允许:

int[] a, b[];

也就是:

int[] a;
int[][] b;

然而,这是一种可怕的编码风格,不应该这样做。

其他回答

Java语言规范说:

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[][];

因此,它们将产生完全相同的字节代码。

是的,有区别。

Int [] a = new Int [100];// 'a'本身不是数组,数组存储为地址 在内存的其他地方,'a'只保存该地址

Int b[] = new Int [100];//当创建像cleary这样的数组时,显示'b'是一个数组,并且它 整数类型。

不,这些是一样的。然而

byte[] rowvector, colvector, matrix[];

等价于:

byte rowvector[], colvector[], matrix[][];

摘自Java规范。这意味着

int a[],b;
int[] a,b;

是不同的。我不推荐这两种声明。最容易读的(可能)是:

int[] a;
int[] b;

是的,完全一样。就我个人而言,我更喜欢

int[] integers; 

因为它让任何阅读你代码的人都能一眼看出integers是一个int型数组,而不是

int integers[];

这并不是很明显,特别是当您在一行中有多个声明时。但同样,它们是相等的,所以这取决于个人偏好。

查看这一页关于Java中的数组的更深入的示例。

它们是一样的。其中一种(对某些人来说)比另一种更具可读性。