gpt4 book ai didi

java - ArrayIndexOutOfBounds 应在边界内的对象上

转载 作者:行者123 更新时间:2023-12-02 09:33:34 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

(26 个回答)


2年前关闭。




我一直坚持让我的程序工作无数个小时,就像我认为我已经完成了所有工作一样,它提出了 ArrayIndexOutOfBounds。我不明白为什么会发生这种情况,因为它只发生在第一个循环 0 0 上。循环本质上是查看一组对象,然后查看每个对象是否被保留(寻找一个开放的飞机座位)。我将发布很多代码,因为我的很多项目都链接到其他类。

当我启动 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;
}


}

座位等级:
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;
}
}

}

大地板网格类:
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;

}

}
}
}

一旦我回答了“您希望通过智能程序或方式程序预订您的座位吗?”,就会出现错误。然后它进入一个 if 语句,其代码为“customerSeat = floorType.queryAvailableFirstClassSeat(theSeatType);”打开 FloorGrid.java 并到达第 135 行,然后出现错误“线程中的异常”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() 方法可以完成我希望他们做的所有事情。他们用座位号绘制整个飞机,如果它是保留的,它在哪个区域等。但是,我真的不知道如何使用我在它扩展的父类(super class) 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;
    }
    }
    }

    关于java - ArrayIndexOutOfBounds 应在边界内的对象上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744214/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com