gpt4 book ai didi

java - 如何将数组的数据直接放入PriorityQueue

转载 作者:行者123 更新时间:2023-12-05 01:47:32 24 4
gpt4 key购买 nike

如何将数组的数据直接放入实现Queue接口(interface)的PriorityQueue中?

ContestantFilter [] contestantFilter = new ContestantFilter [50];
contestantFilter [0] = new ContestantFilter("Agnesy Pricillia","IF110026");
contestantFilter [1] = new ContestantFilter("Ellen Shie ","IF110103");
contestantFilter [2] = new ContestantFilter("Wilma Yue Gunawan","EL110021");
.................
.................

Queue<ContestantFilter> queue = new PriorityQueue<ContestantFilter>(1,
new Comparator<ContestantFilter>() {
@Override
public int compare(ContestantFilter t1, ContestantFilter t2) {
return t1.getID().compareTo(t2.getID());}});

我试过了

for(ContestantFilter c : contestantFilter ){ queue.add(c);}

但是速度很慢。

类似的东西:

queue = ((Queue<ContestantFilter >) Arrays.asList(contestantFilter );

转换不起作用。

最佳答案

使用PriorityQueue#addAll创建队列实例后的方法:

//... is a replacement for your custom Comparator<ContestantFilter>
Queue<ContestantFilter> queue = new PriorityQueue(...);
queue.addAll(Arrays.asList(contestantFilter));

关于java - 如何将数组的数据直接放入PriorityQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com