1

如果我在另一个数组中初始化这些值然后将它传递给主函数,它会起作用。是我做错了什么还是只是我们不能直接传递值?以下是两个代码:- 使用数组传递:-

public class DDArray {
    void array(int[][] a){
        int x=a.length;
        int y=a[0].length;
        for(int i=0;i<x;i++){
            for(int j=0;j<y;j++){
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void main(String args[]){
        DDArray ob=new DDArray();
        int[][] b={{1,2,3,4,5},{11,22,33,44,55}};
        ob.array(b);
    }
}

直接通过:-

public class DDArray {
    void array(int[][] a){
        int x=a.length;
        int y=a[0].length;
        for(int i=0;i<x;i++){
            for(int j=0;j<y;j++){
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void main(String args[]){
        DDArray ob=new DDArray();
        ob.array({{1,2,3,4,5},{11,22,33,44,55}});
    }
}
4

2 回答 2

0

从 ob.array({{1,2,3,4,5},{11,22,33,44,55}}) 直接调用的变化;到 ob.array( new int[][] { { 1, 2, 3, 4, 5 }, { 11, 22, 33, 44, 55 } });

于 2019-10-12T10:31:49.730 回答
-1

要回答您的问题,您不能直接传递这样的值。编译器也会抱怨。编译器错误在这里非常简单 - 此处不允许使用数组初始值设定项。

于 2019-10-12T10:09:55.127 回答