如何仅初始化二维数组的部分?

时间:2016-10-31 02:38:40

标签: java

我不明白为什么这段代码不起作用:

arraySize = input.nextInt();
int [][] twoD = new int [arraySize][arraySize];
twoD = { {1},{2} }; //Error occurs here. The above code works but if I try to initialize it partly it doesn't work? Why's that, how is my syntax wrong?

1 个答案:

答案 0 :(得分:1)

那种语法在那里是不合法的(省略类型是声明的合成糖)。您可以

twoD = new int[][] { {1},{2} }; 

twoD将使用两个长度为1的数组完全初始化(并且数组具有固定长度)。