搜索arraylist时处理关系

时间:2017-04-05 17:02:42

标签: java arraylist

在我的任务中,我必须预订房间预订计划。可以选择计算所制作的房间,并显示最多座位的房间。我想要做的是展示多个房间,如果有两个房间的座位最多(领带)。我现在正在努力打印出座位最多的房间,我对如何实施这种关系感到困惑。

找到最大房间的课程

public static void roomCount(ArrayList<Room> rooms) {
    int largestRoom = 0;
    int largestNumOfRooms = 0;
    for (int i = 0; i < rooms.size(); i++) {
    if (rooms.get(i).getNumberOfSeats() > largestNumOfRooms) {//Determine largest room
            largestNumOfRooms = rooms.get(i).getNumberOfSeats();
            largestRoom = rooms.get(i).getRoomNumber();
        }
    }

房间等级

     public class Room {
Scanner input = new Scanner(System.in);
private int roomNumber;
private int numberOfSeats;
private String reservedBy = "";
private boolean reserved;
private boolean hasSmartBoard;

/**
 * Get the attribute values from the user.
 */

public Room(int roomNumber) {
    this.roomNumber = roomNumber;

}


public void getRoomDetailsFromUser() {

    System.out.print("Enter number of seats: ");
    numberOfSeats = input.nextInt();
    input.nextLine();
    System.out.print("Does this classroom have a smart board? (Y/N)");
    hasSmartBoard = input.nextLine().equalsIgnoreCase("y");

}

public boolean isHasSmartBoard() {
    return hasSmartBoard;
}

public void setHasSmartBoard(boolean hasSmartBoard) {
    this.hasSmartBoard = hasSmartBoard;
}

public int getNumberOfSeats() {
    return numberOfSeats;
}

public void setNumberOfSeats(int numberOfSeats) {
    this.numberOfSeats = numberOfSeats;
}

public String getReservedBy() {
    return reservedBy;
}

public void setReservedBy(String reservedBy) {
    this.reservedBy = reservedBy;
}

public boolean isReserved() {
    return reserved;
}

public void setReserved(boolean reserved) {
    this.reserved = reserved;
}


public int getRoomNumber() {
    return roomNumber;
}

/**
 * Update the room to reserved and get the reserved by.
 */

public void reserveThisRoom(){
    this.reserved = true;
    System.out.println("Enter the name of the person reserving this room: ");
    reservedBy = input.nextLine();
}

 /**
 * Update the room to not reserved and clear the reserved by.
 */

public void releaseThisRoom(){
    this.reserved = false;
    reservedBy = "";
    System.out.println("Room has been released\n");


}

public String toString() {
    String output = "\n\n******************************"
            + "\nRoom Number: " + roomNumber
            + "\nNumber of Seats: " + numberOfSeats
            + "\nReserved By: " + reservedBy
            + "\nReserved: " + reserved
            + "\nSmart Board: "+ hasSmartBoard;
    return output;
}
}

2 个答案:

答案 0 :(得分:0)

而不是在$ sudo docker run -it layer1 AAA 中存储largestRoom,您可以使用int。所以你可以做以下事情

List

此外,您还需要从此方法返回值。

答案 1 :(得分:0)

room-numbers 制作另一个ArrayList。像:

List<Integer> largestRoom = new ArrayList<Integer>();
if (rooms.get(i).getNumberOfSeats() > largestNumOfRooms) {
        largestNumOfRooms = rooms.get(i).getNumberOfSeats();
        largestRoom.clear();
        largestRoom.add(rooms.get(i).getRoomNumber());
}else if(rooms.get(i).getNumberOfSeats()==largestNumOfRooms){
    largest.add(rooms.get(i).getRoomNumber())
}

现在,largestRoom拥有您需要的房间号