简体   繁体   English

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

[英]Find all matching elements in Array - Java

At the moment my code is only giving me the first matching result.目前我的代码只给了我第一个匹配的结果。 The user inputs their desired room price and at the moment it will only display the first match.用户输入他们想要的房间价格,此时它只会显示第一个匹配项。 In the case the user inputs '60' to the console it should display 3 results.在用户向控制台输入“60”的情况下,它应该显示 3 个结果。 I imagine i'll need another forloop and if statement after it prints to the console but not sure how to execute我想在打印到控制台后我需要另一个 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();
    }
}

Any other information needed feel free to ask任何其他需要的信息随时询问

If you only want to print the information move the print commands inside the loop and remove the break ie如果您只想打印信息,请在循环内移动打印命令并删除中断,即

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("-------------------");
    }   
}
            

You could also save all the objects in a list with the first loop and then in a second loop iterate over the list and print the information ie您还可以使用第一个循环将所有对象保存在列表中,然后在第二个循环中遍历列表并打印信息,即

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("-------------------");
}       

I notice 2 things:我注意到两件事:

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

So you are stopping the loop after the first match.所以你在第一场比赛后停止循环。

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

Is there a reason to use that minus 1?有理由使用负1吗?

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

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