ArrayIndexOutOfBounds 在应该在边界内的对象上
ArrayIndexOutOfBounds on a object that should be in bounds
我一直坚持让我的程序运行无数个小时,就在我认为我已经全部运行时,它出现了 ArrayIndexOutOfBounds。我不明白为什么会发生这种情况,因为它仅在第一个循环中发生,即 0 0.A 循环本质上是查看对象数组,然后查看每个对象是否已保留(寻找空位)。我将发布大量代码,因为我的很多项目都链接到其他 类。
当我启动 TrainSeatBookingApplication 时,我按 p、m、f、s 的顺序回答问题。所以请使用该命令进行调试,因为我还没有完全完成我所知道的其他结果。
火车座位预订申请:
package exercises;
import java.util.Scanner;
public class TrainSeatBookingApplication {
public static void main(String[] args) {
SeatType theSeatType;
FloorGrid floorType;
TrainWay aTrainWay = null;
TrainSmart aTrainSmart = null;
Seat customerSeat;
Seat trainSeats;
char planeSizeChoice;
char seatingArea;
char seatEconomyOrFirst;
char programBookingChoice;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to board a petite floor sized plane or a grande floor sized plane?");
planeSizeChoice = Character.toLowerCase(scan.next().charAt(0));
if (planeSizeChoice == 'p') {
floorType = new PetiteFloorGrid();
floorType.initialiseFloorGrid();
System.out.println("Would you like to be in the middle, window or asile?");
seatingArea= Character.toUpperCase(scan.next().charAt(0));
if (seatingArea == 'M') {
theSeatType = SeatType.MIDDLE;
}
else if (seatingArea == 'A') {
theSeatType = SeatType.AISLE;
}
else {
theSeatType = SeatType.WINDOW;
}
System.out.println("Would you like to be seated in first class or economy class?");
seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));
System.out.println("Would you like your seat to be booked via the smart program or the way program?");
programBookingChoice = Character.toUpperCase(scan.next().charAt(0));
if (seatEconomyOrFirst == 'F') {
if (programBookingChoice == 'S') {
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainSmart.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
else {
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainWay.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
}
else {
}
}
else {
floorType = new GrandeFloorGrid();
floorType.initialiseFloorGrid();
System.out.println("Would you like to be in the middle, window or asile?");
seatingArea= Character.toUpperCase(scan.next().charAt(0));
if (seatingArea == 'M') {
theSeatType = SeatType.MIDDLE;
}
else if (seatingArea == 'A') {
theSeatType = SeatType.AISLE;
}
else {
theSeatType = SeatType.WINDOW;
}
System.out.println("Would you like to be seated in first class or middle class?");
seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));
System.out.println("Would you like your seat to be booked via the smart program or the way program?");
programBookingChoice = Character.toUpperCase(scan.next().charAt(0));
//System.out.println("Did not reach start of if");//testing program
if (seatEconomyOrFirst == 'F') {
if (programBookingChoice == 'S') {
customerSeat = new Seat();
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainSmart.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
else {
customerSeat = aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
System.out.println(floorType);
}
//System.out.println("Did not go through either if or else");//testing program
}
}
}
}
地板网格:
package exercises;
abstract class FloorGrid {
protected Seat[][] seat;
protected int nRows;
protected int nColumns;
protected int nFirstClassRows;
abstract protected void initialiseFloorGrid();
public Seat getLeft(Seat seatx)
{
int column = seatx.getSeatPosition().getColumn();
int row = seatx.getSeatPosition().getRow();
column = column - 1;
if (seat[column + 1][row].getSeatType() == seat[column][row].getSeatType()) {
return seat[column][row];
}
else {
return null;
}
}
public Seat getRight(Seat seatx)
{
int column = seatx.getSeatPosition().getColumn();
int row = seatx.getSeatPosition().getRow();
column = column + 1;
if (seat[column - 1][row].getSeatType() == seat[column][row].getSeatType()) {
return seat[column][row];
}
else {
return null;
}
}
ublic Seat queryAvailableFirstClassSeat(SeatType seatx)
{
boolean found = false;
int row;
int column;
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
seat = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
seat = new Seat[x][y];
if (seatx.getSpecificSeatType() == 2) /*2 is middle*/ {
if (!seat[x][y].isReserved()) {
if (seat[x][y].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if(seatx.getSpecificSeatType() == 3) { //3 is windows
if (!seat[x][y].isReserved()) {
if (seat[x][y].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if (seatx.getSpecificSeatType() == 1) { // 1 is aisle
if (!seat[y][x].isReserved()) {
if (seat[y][x].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if (seatx.getSpecificSeatType() == 10) {
if (!seat[y][x].isReserved()) {
if (seat[y][x].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
if (x == (nRows - 1) & y == (nColumns = 1) & found == false) { //this checks to see if the loop is looping through the last seat. If it is and no open seat has been found it returns null
return null;
}
}
}
return null;
}
}
public Seat getSeat(int seatRow, char seatPosition)
{
return null;
}
}
小地板网格:
package exercises;
package exercises;
public class PetiteFloorGrid extends FloorGrid {
Seat[][] newSeats;
public PetiteFloorGrid () {
this.nColumns = 7;
this.nRows = 10;
this.nFirstClassRows = 4;
this.initialiseFloorGrid();
}
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
if (x < 4) {
seat.setFirstClass(true);
}
if (y > 1 & y < 5) {
seat.setSeatType(SeatType.MIDDLE);
}
else if (y < 1 & y > 5) {
seat.setSeatType(SeatType.WINDOW);
}
else {
seat.setSeatType(SeatType.AISLE);
}
SeatPosition aSeatPosition = new SeatPosition(x, (char) ('A' + y));
seat.setSeatPosition(aSeatPosition);;
newSeats[x][y] = seat;
}
}
}
public Seat[][] initialisedSeat() {
return newSeats;
}
}
座位Class:
package exercises;
public class Seat {
private boolean firstClass;
private boolean reserved;
private SeatType seatType;
private SeatPosition seatPosition;
public Seat(SeatPosition seatPosition, SeatType seatType, boolean reserved, boolean firstClass)
{
this.seatPosition = seatPosition;
this.seatType = seatType;
this.reserved = reserved;
this.firstClass = false;
}
public Seat(SeatPosition seatPosition, boolean reserved, boolean firstClass)
{
this.seatPosition = seatPosition;
this.seatType = SeatType.AISLE;
this.reserved = reserved;
this.firstClass = false;
}
public Seat() {
SeatPosition aSeatPosition = new SeatPosition(1,'a');
this.seatPosition = aSeatPosition;
this.seatType = SeatType.AISLE;;
this.reserved = false;
}
public SeatType getSeatType()
{
return this.seatType;
}
public void setSeatType(SeatType seattype) {
this.seatType = seattype;
}
public boolean getFirstClass() {
return this.firstClass;
}
public boolean isFirstClass()
{
if (firstClass == true)
{
return true;
}
else
{
return false;
}
}
public void setFirstClass(boolean trueOrNot) {
this.firstClass = trueOrNot;
}
public boolean isReserved()
{
if (reserved == true)
{
return true;
}
else
{
return false;
}
}
public void setReserved(boolean reserved)
{
this.reserved = reserved;
}
public SeatPosition getSeatPosition()
{
return this.seatPosition;
}
public void setSeatPosition(SeatPosition aSeatPosition) {
this.seatPosition = aSeatPosition;
}
public String toDescription()
{
String typeClass;
String bookedOrNot;
if (firstClass == true)
{
typeClass = "First Class";
}
else
{
typeClass = "Economy Class";
}
if (reserved == true) {
bookedOrNot = "";
}
else {
bookedOrNot = " not ";
}
return ""+typeClass+" "+seatType+"seat at: "+seatPosition.getColumn()+""+seatPosition.getRow()+" is"+bookedOrNot+"booked";
}
public String toString()
{
char reservedOrNot;
char firstClassOrNot;
if (firstClass == true)
{
if (seatType.toString().equals(SeatType.AISLE)) {
firstClassOrNot = 'A';
}
else if (seatType.toString().equals(SeatType.MIDDLE)) {
firstClassOrNot = 'M';
}
else if (seatType.toString().equals(SeatType.WINDOW)) {
firstClassOrNot = 'W';
}
else {
firstClassOrNot = 'X';
}
}
else
{
if (seatType.toString().equals(SeatType.AISLE)) {
firstClassOrNot = 'a';
}
else if (seatType.toString().equals(SeatType.MIDDLE)) {
firstClassOrNot = 'm';
}
else if (seatType.toString().equals(SeatType.WINDOW)) {
firstClassOrNot = 'w';
}
else {
firstClassOrNot = 'x';
}
}
if (reserved == true)
{
reservedOrNot = 'X';
}
else
{
reservedOrNot = '_';
}
return "["+firstClassOrNot+" "+reservedOrNot+"]";
}
}
座位类型:
package exercises;
public enum SeatType {
WINDOW(3),MIDDLE(2),AISLE(1);
private int option;
private SeatType(int option)
{
this.setSeatType(option);
}
private SeatType()
{
}
public int getSeatType()
{
return this.option;
}
public void setSeatType(int option)
{
this.option = option;
}
public int getSpecificSeatType() {
return this.getSeatType();
}
}
智能训练:
package exercises;
public class TrainSmart extends TrainOperator {
private Seat aSeat;
private int foundFClass = 1;
private int foundEClass = 1;
private String sameAsWindow;
private String sameAsAisle;
PetiteFloorGrid aPetiteFloor = new PetiteFloorGrid();
public PetiteFloorGrid getPetiteFloor() {
return this.aPetiteFloor;
}
@Override
public Seat reserveFirstClass(char chosenGrid, SeatType aType) {
if (aType == SeatType.WINDOW) {
sameAsWindow = "yes";
}
else if(aType == SeatType.AISLE) {
sameAsAisle = "yes";
}
System.out.println("Outside If, attempting to enter");
if (chosenGrid == 'P') { //checks if the user specified grid is P for petite, if not carries on untill grand
System.out.println("Inside if");
if (aPetiteFloor.queryAvailableFirstClassSeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
else if (aPetiteFloor.queryAvailableFirstClassSeat(SeatType.WINDOW) != null & sameAsAisle.equals("yes")) {
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(SeatType.WINDOW);
foundFClass = 2;
if (aPetiteFloor.getLeft(aSeat) != null) {
aSeat = aPetiteFloor.getLeft(aSeat);
}
else {
aSeat = aPetiteFloor.getRight(aSeat);
}
aSeat.setReserved(true);
return aSeat;
}
else if (aPetiteFloor.queryAvailableFirstClassSeat(SeatType.AISLE) != null & sameAsWindow.equals("yes")) {
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(SeatType.AISLE);
foundFClass = 2;
if (aPetiteFloor.getLeft(aSeat) != null) {
aSeat = aPetiteFloor.getLeft(aSeat);
}
else {
aSeat = aPetiteFloor.getRight(aSeat);
}
aSeat = aPetiteFloor.getLeft(aSeat);
aSeat.setReserved(true);
return aSeat;
}
return null;
}
else {
GrandeFloorGrid aGrandeFloor = new GrandeFloorGrid();
if (aGrandeFloor.queryAvailableFirstClassSeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
else if (aGrandeFloor.queryAvailableFirstClassSeat(SeatType.WINDOW) != null & sameAsAisle.equals("yes")) {
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(SeatType.WINDOW);
foundFClass = 2;
if (aGrandeFloor.getLeft(aSeat) != null) {
aSeat = aGrandeFloor.getLeft(aSeat);
}
else {
aSeat = aGrandeFloor.getRight(aSeat);
}
aSeat.setReserved(true);
return aSeat;
}
else if (aGrandeFloor.queryAvailableFirstClassSeat(SeatType.AISLE) != null & sameAsWindow.equals("yes")) {
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(SeatType.AISLE);
foundFClass = 2;
if (aGrandeFloor.getLeft(aSeat) != null) {
aSeat = aGrandeFloor.getLeft(aSeat);
}
else {
aSeat = aGrandeFloor.getRight(aSeat);
}
aSeat = aGrandeFloor.getLeft(aSeat);
aSeat.setReserved(true);
return aSeat;
}
return null;
}
}
@Override
public Seat reserveEconomyClass(char chosenGrid, SeatType aType) {
if (aType == SeatType.WINDOW) {
sameAsWindow = "yes";
}
else if(aType == SeatType.AISLE) {
sameAsAisle = "yes";
}
if (chosenGrid == 'P') { //checks if the user specified grid is P for petite, if not carries on untill grand
PetiteFloorGrid aPetiteFloor = new PetiteFloorGrid();
if (aPetiteFloor.queryAvailableEconomySeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aPetiteFloor.queryAvailableEconomySeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
return null;
}
else {
GrandeFloorGrid aGrandeFloor = new GrandeFloorGrid();
if (aGrandeFloor.queryAvailableEconomySeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aGrandeFloor.queryAvailableEconomySeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
return null;
}
}
}
大楼层网格Class:
package exercises;
public class GrandeFloorGrid extends FloorGrid {
Seat[][] newSeats;
public GrandeFloorGrid () {
this.nColumns = 9;
this.nRows = 12;
this.nFirstClassRows = 6;
}
@Override
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
if (x < 6) {
seat.setFirstClass(true);
}
if (y > 2 & y < 6) {
seat.setSeatType(SeatType.MIDDLE);
}
else if (y < 2 & y > 6) {
seat.setSeatType(SeatType.WINDOW);
}
else {
seat.setSeatType(SeatType.AISLE);
}
SeatPosition aSeatPosition = new SeatPosition(x, (char) ('A' + y));
seat.setSeatPosition(aSeatPosition);;
newSeats[x][y] = seat;
}
}
}
}
一旦我回答了 "Would you like your seat to be booked via the smart program or the way program?" 就会出现错误,然后它会进入一个代码为 "customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);" 的 if 语句,它会打开 FloorGrid.java 并到达第 135 行,然后出现错误 "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0".
非常感谢所有的帮助,我现在一整天都在努力解决这个问题。
这个问题还没有人回答过。原因是,因为在那个问题中,人们可以很容易地看出他们有 <= 而应该始终是 <。我的参数是
for (int y = 0; y < nRows; ++y) {
for (int x = 0; x < nColumns; ++x) {
我相信这不是问题。
我已经解决了我的两个错误,但是我还有几个让我困惑的地方。
问题
TrainWay.Java中的这行代码:
aPetiteFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
以及它的等价物(如果用户选择 Grande Floor Size):
aGrandeFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
。
会一直输出这个错误:
java.lang.NullPointerException
其次,如果当被问及我想使用哪个程序来预订我的火车时我选择 w 作为最后一个选项,那么在输入您的输入后,将什么都不做。几乎就像扫描仪正在接受无限输入。
现在经过很多步,如果我没记错的话,TrainWay系统中的这个while循环似乎陷入了无限循环:
while (foundEClass == 1 & (chosen < 4) ) { //This algorithm checks each enum type SeatType and if there is a available seat on each type
if (aPetiteFloor.queryAvailableEconomySeat(aType.values()[+chosen]) != null) {
aSeat = aPetiteFloor.queryAvailableEconomySeat(aType.values()[+chosen]);
aSeat.setReserved(true);
foundEClass = 2;
if (foundEClass == 2) {
return aSeat;
}
}
++chosen;
}
它似乎执行了代码的 while 位,一旦它检查了 if 参数并发现它是假的,那么它就直接重复而不看任何其他代码(++chosen 和其他 if)。
这让我发现最大的问题更多是与编程概念有关。 PetitieFloorGrid.java 和 GrandeFloorGrid.Java 中的 initialiseFloorGrid() 方法执行我希望它们执行的所有操作。他们用座位号、是否保留以及它在什么区域等绘制出整个飞机。但是,我真的不知道如何使用我在它扩展的超类 FloorGrid 中使用 initialiseFloorGrid() 生成的座位。 (FloorGrid 包含 TrainWay.java 中使用的 Query 方法,作为不断循环的 while 循环中的参数。
所以如果我能弄清楚如何在 FloorGrid 中使用在 Petite/GrandeFloorGrid 中创建的座位,那么我就可以修复整个 TrainWay.java 方法。
迭代本身不是问题。看看你是如何初始化地板网格的
@Override
protected void initialiseFloorGrid() {
for (int y = 0; y < nRows + nFirstClassRows; ++y) {
for (int x = 0; x < nColumns; ++x) {
//newSeats[y][x].getSeatPosition().setSeatPosition(nRows, (char) ('A' + nColumns));
newSeats = new Seat[y][x];
newSeats[y][x].setReserved(false);
}
}
}
- 每次都会初始化
newSeats
变量。
- 越界异常表示您正在寻找一个大于实际存在的数组索引。初始化数组然后填充它。
- x - 行
- y - 列
考虑以下几点:
@Override
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
newSeats[x][y] = seat;
}
}
}
我一直坚持让我的程序运行无数个小时,就在我认为我已经全部运行时,它出现了 ArrayIndexOutOfBounds。我不明白为什么会发生这种情况,因为它仅在第一个循环中发生,即 0 0.A 循环本质上是查看对象数组,然后查看每个对象是否已保留(寻找空位)。我将发布大量代码,因为我的很多项目都链接到其他 类。
当我启动 TrainSeatBookingApplication 时,我按 p、m、f、s 的顺序回答问题。所以请使用该命令进行调试,因为我还没有完全完成我所知道的其他结果。
火车座位预订申请:
package exercises;
import java.util.Scanner;
public class TrainSeatBookingApplication {
public static void main(String[] args) {
SeatType theSeatType;
FloorGrid floorType;
TrainWay aTrainWay = null;
TrainSmart aTrainSmart = null;
Seat customerSeat;
Seat trainSeats;
char planeSizeChoice;
char seatingArea;
char seatEconomyOrFirst;
char programBookingChoice;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to board a petite floor sized plane or a grande floor sized plane?");
planeSizeChoice = Character.toLowerCase(scan.next().charAt(0));
if (planeSizeChoice == 'p') {
floorType = new PetiteFloorGrid();
floorType.initialiseFloorGrid();
System.out.println("Would you like to be in the middle, window or asile?");
seatingArea= Character.toUpperCase(scan.next().charAt(0));
if (seatingArea == 'M') {
theSeatType = SeatType.MIDDLE;
}
else if (seatingArea == 'A') {
theSeatType = SeatType.AISLE;
}
else {
theSeatType = SeatType.WINDOW;
}
System.out.println("Would you like to be seated in first class or economy class?");
seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));
System.out.println("Would you like your seat to be booked via the smart program or the way program?");
programBookingChoice = Character.toUpperCase(scan.next().charAt(0));
if (seatEconomyOrFirst == 'F') {
if (programBookingChoice == 'S') {
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainSmart.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
else {
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainWay.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
}
else {
}
}
else {
floorType = new GrandeFloorGrid();
floorType.initialiseFloorGrid();
System.out.println("Would you like to be in the middle, window or asile?");
seatingArea= Character.toUpperCase(scan.next().charAt(0));
if (seatingArea == 'M') {
theSeatType = SeatType.MIDDLE;
}
else if (seatingArea == 'A') {
theSeatType = SeatType.AISLE;
}
else {
theSeatType = SeatType.WINDOW;
}
System.out.println("Would you like to be seated in first class or middle class?");
seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));
System.out.println("Would you like your seat to be booked via the smart program or the way program?");
programBookingChoice = Character.toUpperCase(scan.next().charAt(0));
//System.out.println("Did not reach start of if");//testing program
if (seatEconomyOrFirst == 'F') {
if (programBookingChoice == 'S') {
customerSeat = new Seat();
customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);
aTrainSmart.reserveFirstClass(planeSizeChoice, theSeatType);
System.out.println(floorType);
}
else {
customerSeat = aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
System.out.println(floorType);
}
//System.out.println("Did not go through either if or else");//testing program
}
}
}
}
地板网格:
package exercises;
abstract class FloorGrid {
protected Seat[][] seat;
protected int nRows;
protected int nColumns;
protected int nFirstClassRows;
abstract protected void initialiseFloorGrid();
public Seat getLeft(Seat seatx)
{
int column = seatx.getSeatPosition().getColumn();
int row = seatx.getSeatPosition().getRow();
column = column - 1;
if (seat[column + 1][row].getSeatType() == seat[column][row].getSeatType()) {
return seat[column][row];
}
else {
return null;
}
}
public Seat getRight(Seat seatx)
{
int column = seatx.getSeatPosition().getColumn();
int row = seatx.getSeatPosition().getRow();
column = column + 1;
if (seat[column - 1][row].getSeatType() == seat[column][row].getSeatType()) {
return seat[column][row];
}
else {
return null;
}
}
ublic Seat queryAvailableFirstClassSeat(SeatType seatx)
{
boolean found = false;
int row;
int column;
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
seat = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
seat = new Seat[x][y];
if (seatx.getSpecificSeatType() == 2) /*2 is middle*/ {
if (!seat[x][y].isReserved()) {
if (seat[x][y].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if(seatx.getSpecificSeatType() == 3) { //3 is windows
if (!seat[x][y].isReserved()) {
if (seat[x][y].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if (seatx.getSpecificSeatType() == 1) { // 1 is aisle
if (!seat[y][x].isReserved()) {
if (seat[y][x].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
else if (seatx.getSpecificSeatType() == 10) {
if (!seat[y][x].isReserved()) {
if (seat[y][x].getFirstClass()) {
found = true;
column = seat[x][y].getSeatPosition().getColumn();
row = seat[x][y].getSeatPosition().getRow();
return seat[x][y];
}
}
}
if (x == (nRows - 1) & y == (nColumns = 1) & found == false) { //this checks to see if the loop is looping through the last seat. If it is and no open seat has been found it returns null
return null;
}
}
}
return null;
}
}
public Seat getSeat(int seatRow, char seatPosition)
{
return null;
}
}
小地板网格:
package exercises;
package exercises;
public class PetiteFloorGrid extends FloorGrid {
Seat[][] newSeats;
public PetiteFloorGrid () {
this.nColumns = 7;
this.nRows = 10;
this.nFirstClassRows = 4;
this.initialiseFloorGrid();
}
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
if (x < 4) {
seat.setFirstClass(true);
}
if (y > 1 & y < 5) {
seat.setSeatType(SeatType.MIDDLE);
}
else if (y < 1 & y > 5) {
seat.setSeatType(SeatType.WINDOW);
}
else {
seat.setSeatType(SeatType.AISLE);
}
SeatPosition aSeatPosition = new SeatPosition(x, (char) ('A' + y));
seat.setSeatPosition(aSeatPosition);;
newSeats[x][y] = seat;
}
}
}
public Seat[][] initialisedSeat() {
return newSeats;
}
}
座位Class:
package exercises;
public class Seat {
private boolean firstClass;
private boolean reserved;
private SeatType seatType;
private SeatPosition seatPosition;
public Seat(SeatPosition seatPosition, SeatType seatType, boolean reserved, boolean firstClass)
{
this.seatPosition = seatPosition;
this.seatType = seatType;
this.reserved = reserved;
this.firstClass = false;
}
public Seat(SeatPosition seatPosition, boolean reserved, boolean firstClass)
{
this.seatPosition = seatPosition;
this.seatType = SeatType.AISLE;
this.reserved = reserved;
this.firstClass = false;
}
public Seat() {
SeatPosition aSeatPosition = new SeatPosition(1,'a');
this.seatPosition = aSeatPosition;
this.seatType = SeatType.AISLE;;
this.reserved = false;
}
public SeatType getSeatType()
{
return this.seatType;
}
public void setSeatType(SeatType seattype) {
this.seatType = seattype;
}
public boolean getFirstClass() {
return this.firstClass;
}
public boolean isFirstClass()
{
if (firstClass == true)
{
return true;
}
else
{
return false;
}
}
public void setFirstClass(boolean trueOrNot) {
this.firstClass = trueOrNot;
}
public boolean isReserved()
{
if (reserved == true)
{
return true;
}
else
{
return false;
}
}
public void setReserved(boolean reserved)
{
this.reserved = reserved;
}
public SeatPosition getSeatPosition()
{
return this.seatPosition;
}
public void setSeatPosition(SeatPosition aSeatPosition) {
this.seatPosition = aSeatPosition;
}
public String toDescription()
{
String typeClass;
String bookedOrNot;
if (firstClass == true)
{
typeClass = "First Class";
}
else
{
typeClass = "Economy Class";
}
if (reserved == true) {
bookedOrNot = "";
}
else {
bookedOrNot = " not ";
}
return ""+typeClass+" "+seatType+"seat at: "+seatPosition.getColumn()+""+seatPosition.getRow()+" is"+bookedOrNot+"booked";
}
public String toString()
{
char reservedOrNot;
char firstClassOrNot;
if (firstClass == true)
{
if (seatType.toString().equals(SeatType.AISLE)) {
firstClassOrNot = 'A';
}
else if (seatType.toString().equals(SeatType.MIDDLE)) {
firstClassOrNot = 'M';
}
else if (seatType.toString().equals(SeatType.WINDOW)) {
firstClassOrNot = 'W';
}
else {
firstClassOrNot = 'X';
}
}
else
{
if (seatType.toString().equals(SeatType.AISLE)) {
firstClassOrNot = 'a';
}
else if (seatType.toString().equals(SeatType.MIDDLE)) {
firstClassOrNot = 'm';
}
else if (seatType.toString().equals(SeatType.WINDOW)) {
firstClassOrNot = 'w';
}
else {
firstClassOrNot = 'x';
}
}
if (reserved == true)
{
reservedOrNot = 'X';
}
else
{
reservedOrNot = '_';
}
return "["+firstClassOrNot+" "+reservedOrNot+"]";
}
}
座位类型:
package exercises;
public enum SeatType {
WINDOW(3),MIDDLE(2),AISLE(1);
private int option;
private SeatType(int option)
{
this.setSeatType(option);
}
private SeatType()
{
}
public int getSeatType()
{
return this.option;
}
public void setSeatType(int option)
{
this.option = option;
}
public int getSpecificSeatType() {
return this.getSeatType();
}
}
智能训练:
package exercises;
public class TrainSmart extends TrainOperator {
private Seat aSeat;
private int foundFClass = 1;
private int foundEClass = 1;
private String sameAsWindow;
private String sameAsAisle;
PetiteFloorGrid aPetiteFloor = new PetiteFloorGrid();
public PetiteFloorGrid getPetiteFloor() {
return this.aPetiteFloor;
}
@Override
public Seat reserveFirstClass(char chosenGrid, SeatType aType) {
if (aType == SeatType.WINDOW) {
sameAsWindow = "yes";
}
else if(aType == SeatType.AISLE) {
sameAsAisle = "yes";
}
System.out.println("Outside If, attempting to enter");
if (chosenGrid == 'P') { //checks if the user specified grid is P for petite, if not carries on untill grand
System.out.println("Inside if");
if (aPetiteFloor.queryAvailableFirstClassSeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
else if (aPetiteFloor.queryAvailableFirstClassSeat(SeatType.WINDOW) != null & sameAsAisle.equals("yes")) {
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(SeatType.WINDOW);
foundFClass = 2;
if (aPetiteFloor.getLeft(aSeat) != null) {
aSeat = aPetiteFloor.getLeft(aSeat);
}
else {
aSeat = aPetiteFloor.getRight(aSeat);
}
aSeat.setReserved(true);
return aSeat;
}
else if (aPetiteFloor.queryAvailableFirstClassSeat(SeatType.AISLE) != null & sameAsWindow.equals("yes")) {
aSeat = aPetiteFloor.queryAvailableFirstClassSeat(SeatType.AISLE);
foundFClass = 2;
if (aPetiteFloor.getLeft(aSeat) != null) {
aSeat = aPetiteFloor.getLeft(aSeat);
}
else {
aSeat = aPetiteFloor.getRight(aSeat);
}
aSeat = aPetiteFloor.getLeft(aSeat);
aSeat.setReserved(true);
return aSeat;
}
return null;
}
else {
GrandeFloorGrid aGrandeFloor = new GrandeFloorGrid();
if (aGrandeFloor.queryAvailableFirstClassSeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
else if (aGrandeFloor.queryAvailableFirstClassSeat(SeatType.WINDOW) != null & sameAsAisle.equals("yes")) {
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(SeatType.WINDOW);
foundFClass = 2;
if (aGrandeFloor.getLeft(aSeat) != null) {
aSeat = aGrandeFloor.getLeft(aSeat);
}
else {
aSeat = aGrandeFloor.getRight(aSeat);
}
aSeat.setReserved(true);
return aSeat;
}
else if (aGrandeFloor.queryAvailableFirstClassSeat(SeatType.AISLE) != null & sameAsWindow.equals("yes")) {
aSeat = aGrandeFloor.queryAvailableFirstClassSeat(SeatType.AISLE);
foundFClass = 2;
if (aGrandeFloor.getLeft(aSeat) != null) {
aSeat = aGrandeFloor.getLeft(aSeat);
}
else {
aSeat = aGrandeFloor.getRight(aSeat);
}
aSeat = aGrandeFloor.getLeft(aSeat);
aSeat.setReserved(true);
return aSeat;
}
return null;
}
}
@Override
public Seat reserveEconomyClass(char chosenGrid, SeatType aType) {
if (aType == SeatType.WINDOW) {
sameAsWindow = "yes";
}
else if(aType == SeatType.AISLE) {
sameAsAisle = "yes";
}
if (chosenGrid == 'P') { //checks if the user specified grid is P for petite, if not carries on untill grand
PetiteFloorGrid aPetiteFloor = new PetiteFloorGrid();
if (aPetiteFloor.queryAvailableEconomySeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aPetiteFloor.queryAvailableEconomySeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
return null;
}
else {
GrandeFloorGrid aGrandeFloor = new GrandeFloorGrid();
if (aGrandeFloor.queryAvailableEconomySeat(aType) != null) { //Checks if seat of specified type is free, if so then it books it
aSeat = aGrandeFloor.queryAvailableEconomySeat(aType);
aSeat.setReserved(true);
foundFClass = 2;
return aSeat;
}
return null;
}
}
}
大楼层网格Class:
package exercises;
public class GrandeFloorGrid extends FloorGrid {
Seat[][] newSeats;
public GrandeFloorGrid () {
this.nColumns = 9;
this.nRows = 12;
this.nFirstClassRows = 6;
}
@Override
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
if (x < 6) {
seat.setFirstClass(true);
}
if (y > 2 & y < 6) {
seat.setSeatType(SeatType.MIDDLE);
}
else if (y < 2 & y > 6) {
seat.setSeatType(SeatType.WINDOW);
}
else {
seat.setSeatType(SeatType.AISLE);
}
SeatPosition aSeatPosition = new SeatPosition(x, (char) ('A' + y));
seat.setSeatPosition(aSeatPosition);;
newSeats[x][y] = seat;
}
}
}
}
一旦我回答了 "Would you like your seat to be booked via the smart program or the way program?" 就会出现错误,然后它会进入一个代码为 "customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);" 的 if 语句,它会打开 FloorGrid.java 并到达第 135 行,然后出现错误 "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0".
非常感谢所有的帮助,我现在一整天都在努力解决这个问题。
这个问题还没有人回答过。原因是,因为在那个问题中,人们可以很容易地看出他们有 <= 而应该始终是 <。我的参数是
for (int y = 0; y < nRows; ++y) {
for (int x = 0; x < nColumns; ++x) {
我相信这不是问题。
我已经解决了我的两个错误,但是我还有几个让我困惑的地方。
问题
TrainWay.Java中的这行代码:
aPetiteFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
以及它的等价物(如果用户选择 Grande Floor Size):
aGrandeFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
。
会一直输出这个错误:
java.lang.NullPointerException
其次,如果当被问及我想使用哪个程序来预订我的火车时我选择 w 作为最后一个选项,那么在输入您的输入后,将什么都不做。几乎就像扫描仪正在接受无限输入。
现在经过很多步,如果我没记错的话,TrainWay系统中的这个while循环似乎陷入了无限循环:
while (foundEClass == 1 & (chosen < 4) ) { //This algorithm checks each enum type SeatType and if there is a available seat on each type
if (aPetiteFloor.queryAvailableEconomySeat(aType.values()[+chosen]) != null) {
aSeat = aPetiteFloor.queryAvailableEconomySeat(aType.values()[+chosen]);
aSeat.setReserved(true);
foundEClass = 2;
if (foundEClass == 2) {
return aSeat;
}
}
++chosen;
}
它似乎执行了代码的 while 位,一旦它检查了 if 参数并发现它是假的,那么它就直接重复而不看任何其他代码(++chosen 和其他 if)。
这让我发现最大的问题更多是与编程概念有关。 PetitieFloorGrid.java 和 GrandeFloorGrid.Java 中的 initialiseFloorGrid() 方法执行我希望它们执行的所有操作。他们用座位号、是否保留以及它在什么区域等绘制出整个飞机。但是,我真的不知道如何使用我在它扩展的超类 FloorGrid 中使用 initialiseFloorGrid() 生成的座位。 (FloorGrid 包含 TrainWay.java 中使用的 Query 方法,作为不断循环的 while 循环中的参数。
所以如果我能弄清楚如何在 FloorGrid 中使用在 Petite/GrandeFloorGrid 中创建的座位,那么我就可以修复整个 TrainWay.java 方法。
迭代本身不是问题。看看你是如何初始化地板网格的
@Override
protected void initialiseFloorGrid() {
for (int y = 0; y < nRows + nFirstClassRows; ++y) {
for (int x = 0; x < nColumns; ++x) {
//newSeats[y][x].getSeatPosition().setSeatPosition(nRows, (char) ('A' + nColumns));
newSeats = new Seat[y][x];
newSeats[y][x].setReserved(false);
}
}
}
- 每次都会初始化
newSeats
变量。 - 越界异常表示您正在寻找一个大于实际存在的数组索引。初始化数组然后填充它。
- x - 行
- y - 列
考虑以下几点:
@Override
protected void initialiseFloorGrid() {
int xMax = nRows + nFirstClassRows;
int yMax = nColumns;
newSeats = new Seat[xMax][yMax];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
Seat seat = new Seat();
seat.setReserved(false);
newSeats[x][y] = seat;
}
}
}