数组初始值设定项可用于在编译时初始化数组。如下所示带有尾随逗号的初始值设定项可以正常编译。
int a[][] = {{1,2,} ,{3,4,} , {5,6,},}; //Trailing commas cause no compiler error
for(int i=0;i<a.length;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
输出 :
1 2
3 4
5 6
对于一维数组也是合法的,正如上面的讨论所示。
int[] b = {1, 2, 3, 4, 5, 6,}; //A trailing comma causes no compiler error
for(int i=0;i<b.length;i++)
{
System.out.print(b[i]+"\t");
}
输出 :
1 2 3 4 5 6
即使以下是合法的语法并且编译良好。
int c[][] = {{,} ,{,} , {,},};
编译器应该期望在逗号前后有一个常量值(或另一个初始值设定项),
。这是如何编译的?编译器是否会简单地忽略此类逗号,或者在这种情况下会发生其他情况?