Array2d Java错误

时间:2018-12-09 10:48:51

标签: java arrays

我的源代码:

 public class My {
    public void Array(int[][] numbers) {
        for(int r=0;r<numbers.length;r++){
            for(int c=0;c<numbers[0].length-1;c++){
                if(numbers[r][c+1]>numbers[r][c]){
                    numbers[r][c]=numbers[r][c+1];
                }
            }
        }

    }
    int[][] numbers = {{3,4,5,6},{4,5,6,7},{5,6,7,8}};
    Array(numbers);
}

错误:

/My.java:13: error: invalid method declaration; return type required
    Array(numbers);
    ^
/My.java:13: error: <identifier> expected
    Array(numbers);
                 ^
2 errors

怎么了?

1 个答案:

答案 0 :(得分:0)

您不能在方法之外获得指令。

这是解决问题的一种方法(请注意static关键字):

public class My {
    public static void Array(int[][] numbers) {
        for(int r=0;r<numbers.length;r++){
            for(int c=0;c<numbers[0].length-1;c++){
                if(numbers[r][c+1]>numbers[r][c]){
                    numbers[r][c]=numbers[r][c+1];
                }
            }
        }

    }

    public static final void Main(String[] agrs) {
        int[][] numbers = {{3,4,5,6},{4,5,6,7},{5,6,7,8}};
        Array(numbers);
    }
}