实现comparable或比较器接口需谨慎,不然JDK自带的方法可能不好使了

需求背景:刷题的时候,需求要做排序,并且不能去重,然后写了个 bug。

        所以我用了TreeSet,并且把要排序的内容封装成对象放到集合中,对象实现了compareTo方法。由于我实现的compareTo方法把=0的情况和<0的情况合并了,所以达到了排序且没有去重的效果,但是后面在调用remove方法的时候失败了

        先上一段测试代码:

import java.util.TreeSet;

public class test {
    static class Student implements Comparable{
        public Integer age;
        public String name;

        public Student(Integer age, Strin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Java中的Complex复数类声明实现Comparable<T>接口,提供按模比较复数大小的方法的代码: ```java import java.util.*; public class Complex implements Comparable<Complex>{ private final double re; // the real part private final double im; // the imaginary part // create a new object with the given real and imaginary parts public Complex(double real, double imag) { re = real; im = imag; } // return a string representation of the invoking Complex object public String toString() { if (im == 0) return re + ""; if (re == 0) return im + "i"; if (im < 0) return re + " - " + (-im) + "i"; return re + " + " + im + "i"; } // return abs/modulus/magnitude and angle/phase/argument public double abs() { return Math.hypot(re, im); } // Math.sqrt(re*re + im*im) public double phase() { return Math.atan2(im, re); } // between -pi and pi // return a new Complex object whose value is (this + b) public Complex plus(Complex b) { Complex a = this; // invoking object double real = a.re + b.re; double imag = a.im + b.im; return new Complex(real, imag); } // return a new Complex object whose value is (this - b) public Complex minus(Complex b) { Complex a = this; double real = a.re - b.re; double imag = a.im - b.im; return new Complex(real, imag); } // return a new Complex object whose value is (this * b) public Complex times(Complex b) { Complex a = this; double real = a.re * b.re - a.im * b.im; double imag = a.re * b.im + a.im * b.re; return new Complex(real, imag); } // return a new object whose value is (this * alpha) public Complex scale(double alpha) { return new Complex(alpha * re, alpha * im); } // return a new Complex object whose value is the conjugate of this public Complex conjugate() { return new Complex(re, -im); } // return a new Complex object whose value is the reciprocal of this public Complex reciprocal() { double scale = re*re + im*im; return new Complex(re / scale, -im / scale); } // return the real or imaginary part public double re() { return re; } public double im() { return im; } // return a / b public Complex divides(Complex b) { Complex a = this; return a.times(b.reciprocal()); } // return a new Complex object whose value is the complex exponential of this public Complex exp() { return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)); } // return a new Complex object whose value is the complex sine of this public Complex sin() { return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); } // return a new Complex object whose value is the complex cosine of this public Complex cos() { return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); } // return a new Complex object whose value is the complex tangent of this public Complex tan() { return sin().divides(cos()); } // comparison by modulus public int compareTo(Complex other) { if (this.abs() < other.abs()) { return -1; } else if (this.abs() > other.abs()) { return 1; } else { return 0; } } // test client public static void main(String[] args) { Complex z1 = new Complex(1.0, 1.0); Complex z2 = new Complex(2.0, 2.0); System.out.println(z1.compareTo(z2)); // prints -1 Complex[] arr = {new Complex(3.0, 3.0), new Complex(1.0, 1.0), new Complex(2.0, 2.0)}; Arrays.sort(arr); for (Complex c : arr) { System.out.println(c); } // prints 1.0 + 1.0i // 2.0 + 2.0i // 3.0 + 3.0i } } ``` 在上述代码中,我们实现了`Comparable<Complex>`接口,并提供了`compareTo()`方法,该方法按照复数的模进行比较,并返回-1、0或1,以表示当前对象小于、等于或大于另一个对象。我们使用`Arrays.sort()`方法对一个复数数组进行排序,以演示这个方法的工作原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值