ArrayIndexOutOfBoundsException when initializing a 2dimensional array of objectts

前端 未结 4 1744
盖世英雄少女心
盖世英雄少女心 2021-01-29 02:40

I have a very simple question but i can\'t figure out why I\'m having this exception. I\'m trying to create a 2-dimensional Array of objects for a sudoku puzzle, but when I\'m i

4条回答
  •  攒了一身酷
    2021-01-29 03:17

    The problem is that the default value for an int is 0.

    So when you create your Sudoku object, grid = new Cell[lines][lines]; is equivalent to grid = new Cell[0][0];

    Either change your makeGrid method or provide a size in your constructor.

    public void makeGrid(int size) {
         this.lines = size;
         grid = new Cell[size][size];
         for(int i=0;i

提交回复
热议问题