comparablepairuos.java
来自「国外的数据结构与算法分析用书」· Java 代码 · 共 27 行
JAVA
27 行
package dslib.base;
/** A descendant of PairUos which defines comparison for two pairs whereby the
first items are compared first, and if they are equal, the second items are
compared. */
public class ComparablePairUos extends PairUos implements Comparable
{
/** Constructor of the class
Analysis: Time = O(1) */
public ComparablePairUos(Object o1, Object o2)
{
super(o1, o2);
}
/** Redefine compareTo method by comparing the key values
Analysis: Time = O(1) */
public int compareTo(Object other)
{
Comparable first = (Comparable)((ComparablePairUos)other).firstItem();
Comparable second = (Comparable)((ComparablePairUos)other).secondItem();
if(((Comparable)firstItem()).compareTo(first) == 0)
return ((Comparable)secondItem()).compareTo(second);
else
return ((Comparable)firstItem()).compareTo(first);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?