我需要创建一个用户可以编辑的动态二维数组。我尝试了许多不同的方法,甚至尝试单独进行以更容易诊断,但总是得到一个java.lang.ArrayIndexOutOfBoundsException
. 下面是一些显示问题的代码(不是来自我的项目)。当我尝试用0
.
public class Example {
public static void main (String args[]) {
int rows = 0;
int cols = 0;
int[][] board = new int[rows][cols];
Scanner scan = new Scanner (System.in);
System.out.print("Enter in a row :");
rows = scan.nextInt();
System.out.print("Enter in a col :");
cols =scan.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = 0;
System.out.print ("\t" + board[i][j]);
}
System.out.print ("\n");
}
}
}