Algs4-2.1.21可比较的交易

2.1.21可比较的交易。用我们的Date类(请见2.1.1.4节)作为模板扩展你的Transaction类(请见练习1.2.13),实现Comparable接口,使交易能够按照金额排序。
解答:
public class Transaction implements Comparable<Transaction>
{
 private final double amount;

public int compareTo(Transaction that)
{
   if (this.amount>that.amount) return +1;
   if (this.amount<that.amount) return -1;
   return 0;
}
...
}

posted @ 2018-10-27 08:52  修电脑的龙生  阅读(145)  评论(0编辑  收藏  举报