使用类中的方法更改数组

时间:2017-10-02 11:23:02

标签: java arrays methods return

我尝试了很多来修复这个课但是没有用。我想制作一个酒店(数组),并检查并填写另一个类中定义的人,但我需要添加,删除,检查空项目并使用酒店类内的方法返回他们的索引。问题是当我在方法中更改数组时,我无法返回更改的值,当我尝试添加其他类成员尝试解决时,我以indexOutOfboundryException结束。您是否可以请一看代码并告诉我您是否看到了错误,为什么我无法返回更改后的值以及为什么我将索引排除在边界之外

非常感谢提前!

package com.company;

public class Hotel1 {

    private int numberOfRooms1;  // Number of rooms
    private Person[]bookingList1=new Person[numberOfRooms1];// booking list with initial length of the number of rooms
    private int uniqueId1;
    private Person person1;
    private int currentIndex;
    private boolean isEmpty=true;


    public int getNumberOfRooms1() {
        return numberOfRooms1;
    }

    private Ticket ticket1;

    public Hotel1(int numberOfRoomss1) {// The constructor has one attribute which is the number of the rooms

        this.numberOfRooms1 = numberOfRoomss1;
    }


    //check in method
    public Boolean isEmpty(){
        for (currentIndex=0;currentIndex<bookingList1.length;currentIndex++){
            if (!bookingList1[currentIndex].equals(null))
                 isEmpty=false;
            else  isEmpty= true;
        }

        return isEmpty;
    }
    public int findEmptyRooms(){
        if (isEmpty)
        {
            for (currentIndex=0;currentIndex<bookingList1.length;currentIndex++){
                return currentIndex;
            }
            return currentIndex;
        }
        return currentIndex;

    }
    public Person checkIn1(Person person){
        if (isEmpty==true){
            return bookingList1[findEmptyRooms()]=person;
        }
        else {
            System.out.println("There is no empty rooms");
            return null;
        }


    }

}

2 个答案:

答案 0 :(得分:0)

Aarrghh几行错误......

很抱歉粗鲁,但请查看代码的逻辑,我不是都明白......

例如:     public int findEmptyRooms(){     if(isEmpty)     {         for(currentIndex = 0; currentIndex

}

这个功能应该做什么?!

for (currentIndex=0;currentIndex<bookingList1.length;currentIndex++){
        return currentIndex;
    }

只需始终返回bookingList1.length + 1 (这就是调用checkIn1抛出indexOutOfboundryException的原因)

另一个例子:

public Boolean isEmpty(){
for (currentIndex=0;currentIndex<bookingList1.length;currentIndex++){
    if (!bookingList1[currentIndex].equals(null))
         isEmpty=false;
    else  isEmpty= true;
}

return isEmpty;

}

完全错误,如果数组中的元素为null,后跟非null元素,则isEmpty将返回false

我认为您应该在将问题发布到堆栈溢出

之前重新考虑所有代码

答案 1 :(得分:0)

感谢大家的解答,帮助我重构整个代码并使其更好并让它工作,以防万一你想看看它是如何变成只看一看`package com.company;

public class Hotel1 {
private int numberOfRooms1;// number of rooms
Person rooms[];



public Hotel1(int numberOfRooms1){    //the constructor
    this.numberOfRooms1=numberOfRooms1;
    Person[]rooms=new Person[numberOfRooms1];
    this.rooms=rooms;
}


//Private method to check if the array has an empty place

private boolean isEmpty(){
    boolean isEmpty =false;
    for (int i=0;i<rooms.length;i++){
        if (rooms[i]==null){
            return true;
        }
    }
    return isEmpty;

}

//Check in
public Person[] checkIn(Person person){
    if (isEmpty()){
    for (int i=0;i<rooms.length;i++){
        if (rooms[i]==null){
            rooms[i]=person;
            return rooms;
        }
        else continue;
    }
    }
    else {
        System.out.println("There is rooms left for "+person.getFirstName()+" Sorry!");
    }
    return rooms;
}}