java - java 2d 数组上的 java.lang.ArrayIndexOutOfBoundsException
问题描述
公共类波音座位图扩展座位图{
public BoeingSeatMap() {
this.seats= new Seat[nRows][nColumns] ;
this.nColumns=7;
this.nRows=10;
this.nFirstClassRows=4;
initialiseSeatMap();
}
protected void initialiseSeatMap() {
String str="ABCDEFG";
for (int i=0;i<nRows;i++) {
seats[i][0].getSeatPostion().setRow(i+1);
seats[i][0].getSeatPostion().setColumn(str.charAt(0));
seats[i][0].setSeatType(SeatType.WINDOW);
seats[i][nColumns-1].getSeatPostion().setRow(i+1);
seats[i][nColumns-1].getSeatPostion().setColumn(str.charAt(6));
seats[i][nColumns-1].setSeatType(SeatType.WINDOW);
if (i<=4) {
seats[i][0].setFirstClass(true);
seats[i][nColumns-1].setFirstClass(true);
}
}
只是想知道为什么当我尝试存储到不应超出范围的二维数组的一部分时,这会返回 ArrayIndexOutOfBoundsException,例如 [1][6]
解决方案
您没有包括整个班级,但据我所知,您在“座位”之后实例化了“nColumns”和“nRows”。尝试修改构造函数:
public BoeingSeatMap() {
this.nColumns=7;
this.nRows=10;
this.seats= new Seat[nRows][nColumns] ;
this.nFirstClassRows=4;
initialiseSeatMap();
}
推荐阅读
- angular - 从 ngStyle 的函数返回字符串时未设置样式
- c# - 如果扬声器远离麦克风,Google Speech/NAudio 会有很大的延迟
- python-3.x - 如何将变量与 csv 文件第二列中的值进行比较?
- python - 避免从 PDF 中提取重复图像
- c# - 屏幕截图转换为灰度不起作用。这是什么原因造成的?
- javascript - 拖放后删除的拖放图像
- python - 从 IMDbPy 结果中的电影目录中获取电影 ID
- r - 将 Data.Frame 转换为时间序列对象
- django - Django 管理员用户可以从后端编辑 base.html 页面吗?
- c# - 是否可以将 groupitem 的 itemcount 添加到同一表单的其他地方的单独标签中?