2 Star 0 Fork 0

CS-IMIS-23/zc20172324

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PrioritizedObject.java 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
zc20172324 提交于 7年前 . 书上代码
package chap12;
/**
* PrioritizedObject represents a node in a priority queue containing a
* comparable object, arrival order, and a priority value.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PrioritizedObject<T> implements Comparable<PrioritizedObject>
{
private static int nextOrder = 0;
private int priority;
private int arrivalOrder;
private T element;
/**
* Creates a new PrioritizedObject with the specified data.
*
* @param element the element of the new priority queue node
* @param priority the priority of the new queue node
*/
public PrioritizedObject(T element, int priority)
{
this.element = element;
this.priority = priority;
arrivalOrder = nextOrder;
nextOrder++;
}
/**
* Returns the element in this node.
*
* @return the element contained within the node
*/
public T getElement()
{
return element;
}
/**
* Returns the priority value for this node.
*
* @return the integer priority for this node
*/
public int getPriority()
{
return priority;
}
/**
* Returns the arrival order for this node.
*
* @return the integer arrival order for this node
*/
public int getArrivalOrder()
{
return arrivalOrder;
}
/**
* Returns a string representation for this node.
*
*/
public String toString()
{
return (element + " " + priority + " " + arrivalOrder);
}
/**
* Returns 1 if the this object has higher priority than
* the given object and -1 otherwise.
*
* @param obj the object to compare to this node
* @return the result of the comparison of the given object and
* this one
*/
public int compareTo(PrioritizedObject obj)
{
int result;
if (priority > obj.getPriority())
result = 1;
else if (priority < obj.getPriority())
result = -1;
else if (arrivalOrder > obj.getArrivalOrder())
result = 1;
else
result = -1;
return result;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/zc20172324.git
git@gitee.com:CS-IMIS-23/zc20172324.git
CS-IMIS-23
zc20172324
zc20172324
master

搜索帮助