words.contains(b)求值时未捕获的异常:java.lang.ClassCastException

解决方案 »

  1.   

    main()函数应该改成:
    public static void main(String[] args) {
    try{
    ......}
    catch(Exception e){
    System.out.println(e);
    }
    }
      

  2.   

    TreeSet words = new TreeSet();
    应该为
    Set words = new HashSet();TreeSet是有顺序的集合,所以其中的元素必须可以比较
    也就是说要实现Comparator接口
    你的Word类没有实现它
    因此TreeSet企图将一个Word转化为Comparator时出现类型错误
      

  3.   

    刚才说错了
    是必须实现
    java.lang.Comparable 
    不是
    java.util.Comparator