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

int[]数组 int[]数组

有区别吗?


当前回答

它是从java所基于的C语言中借来的一种替代形式。

有趣的是,在java中有三种方法来定义一个有效的main方法:

public static void main(String[] args) public static void main(字符串args[]) public static void mainargs)

其他回答

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

不,这些是一样的。然而

byte[] rowvector, colvector, matrix[];

等价于:

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

摘自Java规范。这意味着

int a[],b;
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[][];

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

没有真正的区别;然而,

double[] items = new double[10];

首选,因为它清楚地表明类型是数组。

这两个命令是一样的。

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

int[] arrayOne, arrayTwo; //both arrays

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

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