Java:将队列作为方法参数传递?

Java: passing queue as a method argument?

我需要创建 Event class 和 Venue class。

Venueclass中,我需要放入优先队列。我需要编写一个方法,从队列中删除并显示一个事件,并显示一些简单的统计数据:每个事件的平均人数等。

我坚持第一点 - 一种将删除并显示此事件的方法。是否可以将整个队列作为参数传递给方法? - 我试过了,但似乎没有用。 - (显示方法在Eventclass).

public class Event {

    private String name;
    private int time;
    private int numberOfParticipants;

    public Event(String name, int time, int numberOfParticipants) {
        this.name = name;
        this.time = time;
        this.numberOfParticipants = numberOfParticipants;
    }

   /**Getters and setters omitted**/

    @Override
    public String toString() {
        return "Wydarzenie{" +
                "name='" + name + '\'' +
                ", time=" + time +
                ", numberOfParticipants=" + numberOfParticipants +
                '}';
    }

    public void display(PriorityQueue<Event> e){
        while (!e.isEmpty()){
            System.out.println(e.remove());
        }
    }
}

地点Class:

public class Venue {
    public static void main(String[] args) {
         PriorityQueue<Event> pq = new PriorityQueue<>(Comparator.comparing(Event::getTime));
         pq.add(new Event("stand up", 90, 200));
         pq.add(new Event("rock concert", 120, 150));
         pq.add(new Event("theatre play", 60, 120));
         pq.add(new Event("street performance", 70, 80));
         pq.add(new Event("movie", 100, 55));
    }
}

以下是场地的一些变化 class。

class Venue {
    PriorityQueue<Event> pq = new PriorityQueue<Event>(Comparator.comparing(Event::getTime));

    public static void main(String[] args) {
        Venue v = new Venue();
        v.addEvents();
        v.display(v.pq);
    }

    private void addEvents() {
        pq.add(new Event("stand up", 90, 200));
        pq.add(new Event("rock concert", 120, 150));
        pq.add(new Event("theatre play", 60, 120));
        pq.add(new Event("street performance", 70, 80));
        pq.add(new Event("movie", 100, 55));
    }

    private void display(PriorityQueue<Event> e) {
        while (!e.isEmpty()) {
            System.out.println(e.remove());
        }
    }
}

队列处于 class 级别,因此每个 Venue 都可以有自己的队列。 main 方法只是调用其他方法,但理想情况下应该放在不同的 class 中。该显示将在 Venue 实例上调用,您可以在该方法中进行统计,同时从队列中删除每个项目。