程序的参数存储在aa[0]
这是一个数组,因为aa
是一个数组数组。
所以程序只是真正迭代main
方法的参数。它打印1, 2, 3
(它不关心aa[1]
)。
int x;
String aa[][]= new String[2][2]; // create an matrix of size 2x2
aa[0]=a; // store the program arguments into the first row of aa
x=aa[0].length; // store the length of aa[0] which is the same as a
for(int y=0;y<x;y++) // iterate over aa[0] which is the same as a
System.out.print(" "+aa[0][y]);
是相同的功能为:
for (int i = 0; i < a.length; ++i)
System.out.print(" " + a[i]);
// or even
for (String str: a)
System.out.print(" " + str);
编辑
如前所述有人自删除他的回答(你不应该有,我upvoting它,而你删除它),java多维数组是锯齿形数组,这意味着多维数组不必具有相同的大小,您可以使行1和行2具有2种不同的大小。因此,这意味着声明String[2][2]
并不意味着在重新分配行时该行只需限制在两列中。
String[][] ma = new String[3][2];
ma[0] = new String[] {"a", "b"};
ma[1] = new String[] {"a", "b", "c", "d"}; // valid
String[] foo = new String {"1", "3", "33", "e", "ff", "eee"};
ma[2] = foo; // valid also
你在调试器中试过它,看看这是怎么发生的? – djechlin
我使用命令提示符执行此操作 – Ravi
@coders然后在调试器中运行它并查看它在做什么。低调的研究努力。 – djechlin