尝试将 char 数组作为 java 中的参数传递并收到索引错误

top*_*nal 2 java arrays char

我正在尝试绘制并初始化二维数组,当我的所有代码都在主方法中时,没有错误,但我将代码拆分为方法,并且出现索引错误。

这是我的方法:

public static void initializeTable(char[][] table ) {

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
             table[i][j] = 'S';   //this is line 90 where the error occurs i think.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何在 main 中使用它:

    public class Cinema {
    public static int row, column,x,y;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[][] table = new char[row][column];

        enterRowColumn();
        initializeTable(table); //line 15
    }
}
Run Code Online (Sandbox Code Playgroud)

和错误消息:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at cinema.Cinema.initializeTable(Cinema.java:90)
    at cinema.Cinema.main(Cinema.java:15)
Run Code Online (Sandbox Code Playgroud)

Cha*_*ire 6

原始数字的默认值为0

public static int row, column,x,y;
Run Code Online (Sandbox Code Playgroud)

这些都被初始化为零。然后你创建数组

char[][] table = new char[row][column];
Run Code Online (Sandbox Code Playgroud)

在分配其他值之前,数组看起来像char[0][0].

为和赋值移动数组初始化rowcolumn