2017-05-28 147 views
2

给定一个名为类是ThisClass只包含这个:Java数组声明混乱

public static void main(String[][] args) { 
    System.out.println(args[0][1]); 
} 
public static void main(String[] args) { 
    ThisClass app = new ThisClass(); 
    String[][] newargs = {args}; 
    app.main(newargs); 
} 

如果你编译它,然后用java ThisClass a b c运行它打印: b

...所以它的服用第一个数组并自动包装它以适应2d数组?这很奇怪。有人可以打破这里发生的事情吗?我很确定我错过了一些东西。

+2

在java中确实没有任何'2d'数组,只是数组的数组。没有自动换行,你需要一个数组,然后使用数组字面值语法将它固定在另一个数组中。因此你创建了一个数组数组。 – pvg

回答

1

第二个main函数被调用(以String[]作为参数)。

在此功能中,您将创建newArgs为仅包含一个元素的2D数组,该元素为数组{a, b, c}

因此,当您打印args[0][1],打印位置的元素阵列{a, b, c},这是b的指数1

1

System.out.println(args[0][1]);中,args[0]相同String[]

public static void main(String[] args) { 
    ThisClass app = new ThisClass(); 
    String[][] newargs = {args}; 
    app.main(newargs); 
} 

newargs由于包含一个元件,所述String[] args。因此,你是printlng args[1]其中b