我对以下示例有疑问:
public static void main(String[] args) {
// this works
int[] test1DArray = returnArray();
int[][] test2DArray = new int[][] {
// this does not work
new int[]= returnArray(),
// yet this does
new int[] {1, 2 ,3}
}
private static int[] returnArray() {
int[] a = {1, 2, 3};
return a;
}
我正在寻找一种创建二维数组的方法,并将第二维作为从方法返回的数组。我不明白为什么它不起作用,因为我在 Eclipse 中遇到的错误是
The left-hand side of an assignment must be a variable
根据我的理解,我正在创建一个新的 int 数组并将返回值分配给它。像这样立即填充第二维数组
new int[] {1, 2 ,3}
工作起来很有魅力,我希望对从 returnArray()
非常感谢任何帮助。
p/
最佳答案
只需使用:
int[][] test2DArray = new int[][] {
returnArray(),
new int[] {1,2 ,3}
};
关于java - 用函数返回的数组初始化的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44370203/