第十章作业

第八题

集合排序可以用java.lang.Comparable接口或者java.util.Comparator接口来实现。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//第一种方法  comparable接口
//class Plural implements Comparable<Plural> {
//
//  private double x;
//  private double y;   
//  public double getX() {
//      return x;
//  }
//  public void setX(double x) {
//      this.x = x;
//  }
//  public double getY() {
//      return y;
//  }
//  public void setY(double y) {
//      this.y = y;
//  }
//  public Plural() {
//      x = 0; y = 0;
//  }
//  public Plural(double xx, double yy) {
//      x = xx; y = yy;
//  }
//  public void show() {
//      if(y == 0)
//          System.out.println(x);
//      else
//          System.out.println(x + " + " + y + "i");
//  }
//  @Override
//  public int compareTo(Plural o) {
//      // TODO Auto-generated method stub
//      return this.getX() < o.getX() ? -1 : 1 ;
//  }
//  
//}

//第二种方法 Comparator
class Plural{

    private double x;
    private double y;   
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public Plural() {
        x = 0; y = 0;
    }
    public Plural(double xx, double yy) {
        x = xx; y = yy;
    }

    public void show() {
        if (y == 0)
            System.out.println(x);
        else
            System.out.println(x + " + " + y + "i");
    } 
}
class PluralComparator implements Comparator<Plural> {
    @Override
    public int compare(Plural o1, Plural o2) {
        return o1.getX() < o2.getX() ? -1 : 1;
    }
}
public class Q_8 {

    public static void main(String[] args) {
        List<Plural> lst = new ArrayList<>();
        lst.add(new Plural(2.1, 3.5));
        lst.add(new Plural(1.4, 8.2));
        lst.add(new Plural(3.4, 2));

        //Collections.sort(lst, Collections.reverseOrder());
        //Collections.sort(lst);

        Comparator<Plural> ascComparator = new PluralComparator();
//      Collections.sort(lst, ascComparator);
        Comparator<Plural> descComparator = Collections.reverseOrder(ascComparator);
        Collections.sort(lst, descComparator);

        for(Plural p : lst)
            p.show();
    }

}

第十题

这题我使用HashMap来储存, 以Class为key,以String为value
import java.util.HashMap;

class ExceptionCol{
    @SuppressWarnings("rawtypes")
    public HashMap<Class, String> hm = new HashMap<>();
    public ExceptionCol() {
        hm.put(NullPointerException.class, "空指针异常");
        hm.put(ArithmeticException.class, "除数为0");
    }
    public void add(Class c, String s) {
        hm.put(c, s);
    }
}
public class Q_10 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExceptionCol ec=  new ExceptionCol();
        Class c = NullPointerException.class;
        System.out.println(ec.hm.containsKey(c));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值