繁体   English   中英

查找数组中的所有匹配元素 - Java

[英]Find all matching elements in Array - Java

目前我的代码只给了我第一个匹配的结果。 用户输入他们想要的房间价格,此时它只会显示第一个匹配项。 在用户向控制台输入“60”的情况下,它应该显示 3 个结果。 我想在打印到控制台后我需要另一个 forloop 和 if 语句,但不确定如何执行

public static void secondMain() {
    BufferedReader reader;
    var lines = new ArrayList<String>();
    var rooms = new ArrayList<Room>();
    Room selectedRoom = null;

    try {
        reader = new BufferedReader(new FileReader("rooms.txt"));
        String line = reader.readLine();
        lines.add(line);
        while (line != null) {
            line = reader.readLine();
            lines.add(line);

        }

        reader.close();

        for (int i = 0; i < lines.size() - 1; i++) {
            String[] words = lines.get(i).split(" ");
            var room = new Room();
            room.roomNum = Integer.parseInt(words[0]);
            room.roomType = (words[1]);
            room.roomPrice = Double.parseDouble(words[2]);
            room.hasBalcony = Boolean.parseBoolean(words[3]);
            room.hasLounge = Boolean.parseBoolean(words[4]);
            room.eMail = (words[5]);
            rooms.add(room);
        }

        var searchRoomPrice = input.nextDouble();

        for (int i = 0; i < rooms.size(); i++) {
            if (rooms.get(i).roomPrice == searchRoomPrice) {
                selectedRoom = rooms.get(i);
                break;
            }
        }

        System.out.println("Room Number: " + selectedRoom.roomNum);
        System.out.println("Room Type: " + selectedRoom.roomType);
        System.out.println("Room Price: " + selectedRoom.roomPrice);
        System.out.println("Balcony: " + selectedRoom.hasBalcony);
        System.out.println("Lounge: " + selectedRoom.hasLounge);
        System.out.println("Email: " + selectedRoom.eMail);
        System.out.println("-------------------");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

任何其他需要的信息随时询问

如果您只想打印信息,请在循环内移动打印命令并删除中断,即

for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         selectedRoom = rooms.get(i);
         System.out.println("Room Number: " + selectedRoom.roomNum);
         System.out.println("Room Type: " + selectedRoom.roomType);
         System.out.println("Room Price: " + selectedRoom.roomPrice);
         System.out.println("Balcony: " + selectedRoom.hasBalcony);
         System.out.println("Lounge: " + selectedRoom.hasLounge);
         System.out.println("Email: " + selectedRoom.eMail);
         System.out.println("-------------------");
    }   
}
            

您还可以使用第一个循环将所有对象保存在列表中,然后在第二个循环中遍历列表并打印信息,即

List<Room> roomList = new ArrayList<Room>();
for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         roomList.add(rooms.get(i));             
    }   
}
for(Room room : roomList){
    System.out.println("Room Number: " + room.roomNum);
    System.out.println("Room Type: " + room.roomType);
    System.out.println("Room Price: " + room.roomPrice);
    System.out.println("Balcony: " + room.hasBalcony);
    System.out.println("Lounge: " + room.hasLounge);
    System.out.println("Email: " + room.eMail);
    System.out.println("-------------------");
}       

我注意到两件事:

  1. 您正在使用break; 这里:
                if(rooms.get(i).roomPrice == searchRoomPrice){
                    selectedRoom = rooms.get(i);
                    break;
                }   

所以你在第一场比赛后停止循环。

  1. 这里:
for (int i = 0; i < lines.size() - 1; i++)

有理由使用负1吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM