所以我有一个2d数组,我在测试器类中创建,然后我试图发送它并在构造函数中创建一个副本,但得到一个null错误。我哪里错了? 构造函数:
public TheaterSeatSeller(int[][] newSeats)
{
for(int i=0; i<newSeats.length; i++)
{
for(int j=0; j<newSeats[i].length; j++)
{
seats[i][j]=newSeats[i][j];
}
}
}
然后是测试者类
public static void main(String[] args){
//initialize the available seats
int[][] emptySeats = {
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{20,20,30,30,40,40,30,30,20,20},
{20,30,30,40,50,50,40,30,30,20},
{30,40,50,50,50,50,50,50,40,30}};
TheaterSeatSeller mySeats = new TheaterSeatSeller(emptySeats);
}
答案 0 :(得分:0)
您必须在分配值之前初始化seat数组。这应该解决它。
public TheaterSeatSeller(int[][] newSeats) {
seats = new int[newSeats.length][newSeats[0].length];
for (int i = 0; i < newSeats.length; i++) {
for (int j = 0; j < newSeats[i].length; j++) {
seats[i][j] = newSeats[i][j];
System.out.println(seats[i][j]);
}
}
}