我不明白为什么这段代码不起作用:
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?
答案 0 :(得分:1)
那种语法在那里是不合法的(省略类型是声明的合成糖)。您可以
twoD = new int[][] { {1},{2} };
但twoD
将使用两个长度为1
的数组完全初始化(并且数组具有固定长度)。