Java帮助:优先队列

发布于 12-04 03:37 字数 891 浏览 2 评论 0原文

我有一个名为 jobType 的类

package pcs_assignment_2;

public class JobType
{
    private int ID;
    private double aTime;
    private double sTime;

    public JobType(int ID, double aTime, double sTime)
    {
        this.ID = ID;
        this.aTime = aTime;
        this.sTime = sTime;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public double getaTime() {
        return aTime;
    }

    public void setaTime(double aTime) {
        this.aTime = aTime;
    }

    public double getsTime() {
        return sTime;
    }

    public void setsTime(double sTime) {
        this.sTime = sTime;
    }

    public String toString() {
        return "JobType{" + "ID=" + ID + "aTime=" + aTime + "sTime=" + sTime + '}';
    }

}

现在我想要的是 JobType 类型的 PriorityQue,它根据 sTime 对队列进行排序。是否可以?

I have a class called jobType

package pcs_assignment_2;

public class JobType
{
    private int ID;
    private double aTime;
    private double sTime;

    public JobType(int ID, double aTime, double sTime)
    {
        this.ID = ID;
        this.aTime = aTime;
        this.sTime = sTime;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public double getaTime() {
        return aTime;
    }

    public void setaTime(double aTime) {
        this.aTime = aTime;
    }

    public double getsTime() {
        return sTime;
    }

    public void setsTime(double sTime) {
        this.sTime = sTime;
    }

    public String toString() {
        return "JobType{" + "ID=" + ID + "aTime=" + aTime + "sTime=" + sTime + '}';
    }

}

Now what I wanna have is a PriorityQue of Type JobType which sorts the queue on the basis of sTime. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诠释孤独2024-12-11 03:37:16

需要编写一个自定义比较器,请参见下面的代码。
这里队列的初始容量为10。

    new PriorityQueue<JobType>(10, new Comparator<JobType>() {

    @Override
    public int compare(JobType o1, JobType o2) {
             return Double.compare(o1.sTime,o2.sTime); 
    }
    });

A custom comparator needs to be written, see the code below.
here 10 in the initial capacity of the queue.

    new PriorityQueue<JobType>(10, new Comparator<JobType>() {

    @Override
    public int compare(JobType o1, JobType o2) {
             return Double.compare(o1.sTime,o2.sTime); 
    }
    });
剩一世无双2024-12-11 03:37:16

除了比较器选项之外,我相信只需让您的类实现 Comparable 接口就可以实现相同的行为;仅当此顺序模仿对象的自然顺序时才这样做。

Apart from the comparator option, I believe it would be possible to achieve the same behavior by just making your class implement the Comparable interface; do so only if this ordering mimics the natural ordering of your object.

丘比特射中我2024-12-11 03:37:16

您可以有一个简单的列表,其中包含所有作业,然后在要取出一个作业之前,使用 sTime 上的比较函数对列表进行排序。

You could have a simple List where all the jobs reside in and then before you want to take one out, sort the list using a compare function on the sTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文