Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用类内的方法更改数组_Java_Arrays_Methods_Return - Fatal编程技术网

Java 使用类内的方法更改数组

Java 使用类内的方法更改数组,java,arrays,methods,return,Java,Arrays,Methods,Return,我尝试了很多来修复这个类,但是没有成功。我想创建一个hotel(数组),并用另一个类中定义的人来检查和填充它,但是我需要添加、删除、检查空项,并使用hotel类中的方法返回它们的索引。问题是,当我在一个方法中更改数组时,我无法返回更改后的值,当我尝试添加其他类成员试图解决该问题时,我以indexOutOfboundryException结束。你能看一下代码并告诉我,如果你看到错误,为什么我不能返回更改后的值,为什么我得到的索引超出了边界 非常感谢 package com.company; pu

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


    }

}
package.com公司;
公共级酒店1{
private int numberOfRooms1;//房间数
私人人员[]预订列表1=新人[numberOfRooms1];//带有房间数初始长度的预订列表
私人国际唯一性1;
私人1;
私有int-currentIndex;
私有布尔值isEmpty=true;
public int getNumberOfRooms1(){
返回房间数量1;
}
私家票1;
public Hotel1(int numberOfRoomss1){//构造函数有一个属性,即房间数
this.numberOfRooms1=numberOfRooms1;
}
//值机方法
公共布尔值为空(){

对于(currentIndex=0;currentIndexAarrghh),在几行中有这么多错误

很抱歉这么粗鲁,但请检查一下代码的逻辑,我不太明白

例如: 公共int findEmptyRooms(){ 如果(我是空的) { 对于(currentIndex=0;currentIndex }

这个函数应该做什么

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

for(currentIndex=0;currentIndex感谢大家的回答,这些回答帮助我重构了整个代码,并使其更好地工作,以防您想看看它是如何变成的,请看一看`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;
}}
公共级酒店1{
private int numberOfRooms1;//房间数
单人房[];
公共酒店1(int numberOfRooms1){//建造商
此参数。numberOfRooms1=numberOfRooms1;
人员[]房间=新人员[房间数量1];
这个。房间=房间;
}
//用于检查数组是否有空位置的私有方法
私有布尔isEmpty(){
布尔isEmpty=false;

对于(int i=0;iHint:删除所有不需要的实例变量其他方法都需要所有实例。唯一的问题是,当我尝试从主方法测试方法时,无法使用这些方法更改数组否它们不是。您滥用实例变量是为了不声明局部变量(IMHO)不需要所有的实例变量。例如,你不应该有变量<代码>私有布尔是空的/CODE >作为一个字段。如果你想知道是否有空的房间,你应该使用这个方法是空的,而不是一些状态变量甚至可能不是最新的。你的方法也不考虑有AR的情况。e完全没有房间(大小为0的阵列),这可能会导致错误。谢谢,我非常感谢你的回答。你能解释一下为什么你提到的前四个循环总是返回bookingList.length+1吗?Tuco@TheDancerInTheRain实际上,它总是返回0。因为它总是在第一次迭代中返回,没有任何条件。我认为您的indexOutOfboundryException I因为在isEmpty函数中使用了变量currentIndex,所以引发了:该变量的值为bookingList1.length+1,因此findEmptyRooms可以返回超出范围的值,因为这两个函数共享相同的值variable@Tuco,@thedancerain:
private Person[]booking list 1=新人[numberOfRooms1];
不完美但更好:)(删除无用的继续签入,我建议创建一个函数searchEmpty,它类似于isEmpty,但不是返回布尔值,而是返回空房间的索引,以便在此之后,您可以简化最大值的函数签入)
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;
}}