我最近一直在思考定义数组的两种方式之间的区别:
int[]数组 int[]数组
有区别吗?
我最近一直在思考定义数组的两种方式之间的区别:
int[]数组 int[]数组
有区别吗?
当前回答
是的,有区别。
Int [] a = new Int [100];// 'a'本身不是数组,数组存储为地址 在内存的其他地方,'a'只保存该地址
Int b[] = new Int [100];//当创建像cleary这样的数组时,显示'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, b[];
也就是:
int[] a;
int[][] b;
然而,这是一种可怕的编码风格,不应该这样做。
在声明单个数组引用时,它们之间没有太大区别。所以下面两个声明是一样的。
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
当声明多个数组引用时,我们可以找到它们之间的区别。下面两句话的意思是一样的。事实上,这取决于程序员遵循哪一个。但是建议使用标准的Java表示法。
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
它们在语义上是相同的。添加int array[]语法只是为了帮助C程序员适应java。
Int[]数组更可取,而且更容易混淆。
它们是一样的。其中一种(对某些人来说)比另一种更具可读性。