【问题标题】:Changing an array using a method inside a class使用类中的方法更改数组
【发布时间】:2018-03-13 11:11:41
【问题描述】:

我尝试了很多方法来修复这个类,但没有奏效。我想创建一个酒店(数组)并检查并用另一个类中定义的人填充它,但我需要添加、删除、检查空项目并使用酒店类中的方法返回它们的索引。问题是当我在一个方法中更改数组时,我无法返回更改后的值,当我尝试添加其他类成员试图解决它时,我以 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;
        }


    }

}

【问题讨论】:

  • 提示:删除所有不需要的实例变量
  • 其他方法需要所有实例。唯一的问题是当我尝试从 main 方法测试方法时,我无法使用这些方法更改数组
  • 不,他们不是。您正在滥用实例变量以不声明本地变量(恕我直言)。
  • 没有。并非所有实例变量都是必需的。例如,您根本不应该将变量 private boolean isEmpty 作为字段。如果你想知道是否有空房间,你应该使用方法 isEmpty 而不是一些甚至可能不是最新的状态变量。此外,您的大多数方法都没有考虑根本没有房间(大小为 0 的数组)的情况,这可能会导致错误。

标签: java arrays methods return


【解决方案1】:

感谢大家的回答,他们帮助我重构了整个代码,让它变得更好,让它工作,以防万一你想看看它是如何变成的,看看`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;
}}

【讨论】:

  • 不完美但要好得多:)(删除checkIn中无用的继续,我建议创建一个函数searchEmpty,它类似于isEmpty,但不是返回布尔值,而是返回空房间的索引,所以之后也就是说,您可以最大程度地简化您的功能 checkIn)
【解决方案2】:

aarrghh在很少的线条中有这么多错误...

抱歉粗鲁,但请检查您的代码逻辑,我不明白...

例如: 公共 int findEmptyRooms(){ 如果(是空的) { 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

我认为您应该在将问题发布到堆栈溢出之前重新考虑您的所有代码

【讨论】:

  • 非常感谢您的回答 Tuco
  • 你能解释一下为什么你谈到的前四个循环总是返回 bookingList.length +1?Tuco
  • @TheDancerInTheRain 实际上它总是返回 0。因为它总是在第一次迭代中返回,没有任何条件。
  • 我认为你的 indexOutOfboundryException 被抛出是因为你在 isEmpty 函数中使用了变量 currentIndex :该变量取值 bookingList1.length + 1 所以你的 findEmptyRooms 可以返回一个超出范围的值,因为这两个函数共享同一个变量
  • @Tuco, @TheDancerInTheRain: private Person[]bookingList1=new Person[numberOfRooms1];
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 2018-09-20
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多