最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

尝试按存储节点的值之一对优先级队列进行排序

SEO心得admin46浏览0评论
本文介绍了尝试按存储节点的值之一对优先级队列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我是优先队列的新手。在我要实现的算法中,我想根据存储的节点的functionValue对优先级队列进行排序。我不了解优先级队列如何知道根据该值对节点进行排序,而不是我的节点对象的其他八个实例变量之一。我很确定我定义了一个Comparator对象来定义比较/排序规则,但是我不能为Oracle Comparator的Oracle类库做头或尾。

So I'm new to priority queues. In an algorithm I'm trying to implement, I want to sort a priority queue according to the stored nodes' functionValue. I don't understand how the priority queue will know to sort the nodes by that value, as opposed to one of the other eight instance variables of my node objects. I'm pretty sure I define a Comparator object to define the comparison/ sorting rules, but I can't make heads or tails of the Oracle class library for Comparator.

这是我的节点类的属性

public class Node{ public char label; //Holds char for the Move used; U, D, L, R public boolean visited = false; public State nodeState; //Each node holds a State object int depth; int heuristicCount; int functionCount; <--- This is the property I want the priority queue to sort by. . . .

推荐答案

比较器是一个非常简单的界面。您遇到什么困难?关键位是 比较方法。您只需实现它即可比较您的类的两个实例。

Comparator is a pretty simple interface. What's your difficulty with it? The key bit is the compare method. You simply implement that to compare two instances of your class. Something like:

public class NodeComparator implements Comparator<Node> { public int compare(Node a, Node b) { Integer aCount = a.getFunctionCount(); Integer bCount = b.getFunctionCount(); return apareTo(b); } }
发布评论

评论列表(0)

  1. 暂无评论