作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
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;
}
}
}
}
for (int y = 0; y < nRows; ++y) {
for (int x = 0; x < nColumns; ++x) {
aPetiteFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
aGrandeFloor.queryAvailableFirstClassSeat(aType.values()[+chosen]) != null
.
java.lang.NullPointerException
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;
}
最佳答案
迭代本身不是问题。看看你是如何初始化地板网格的
@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
变量每次都被初始化。 @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/
我是一名优秀的程序员,十分优秀!