【问题标题】:Class Cast Exception problems with TreeSetTreeSet 的类转换异常问题
【发布时间】:2019-04-18 20:19:08
【问题描述】:

好的,我有这个问题要解决:

创建一个名为VirtualLibrary 的通用类,它具有单个属性totalNumberOfEntries,并具有使用户能够设置和返回条目的方法。条目类型为BookArticleMediaResourceMagazineManual。为每种类型的条目实现特定的类。 在main( ) 方法中创建一个SortedSet<VirtualLibrary> 变量来维护所有库条目。使用方法添加,添加多个,返回特定条目并检查库中是否存在条目。

import java.util.Collections;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;

class VirtualLibrary<T>{
    private int totalNumberOfEntries;
    void setTotalNumberOfEntries(int totalNumberOfEntries) {
        this.totalNumberOfEntries=totalNumberOfEntries;
    }
    int getTotalNumberOfEntries() {
        return this.totalNumberOfEntries;
    }
}
class Base{}
class Book extends Base{
}
class Article extends Base{ 
}
class MediaResource extends Base{
}
class Magazine extends Base{
}
class Manual extends Base{
}

public class P5 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        VirtualLibrary<Book> book = new VirtualLibrary<>();
        SortedSet<VirtualLibrary> lib = new TreeSet<>();
        int totalNumberOfEntries;
        System.out.println("Please insert the total number of entries of the book: ");
        totalNumberOfEntries=keyboard.nextInt();
        book.setTotalNumberOfEntries(totalNumberOfEntries);
        lib.add(book);
        VirtualLibrary<Article> article = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the article: ");
        totalNumberOfEntries=keyboard.nextInt();
        article.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<MediaResource> mediaResource = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the media resource: ");
        totalNumberOfEntries=keyboard.nextInt();
        mediaResource.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<Magazine> magazine = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the magazine: ");
        totalNumberOfEntries=keyboard.nextInt();
        magazine.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<Manual> manual = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the manual: ");
        totalNumberOfEntries=keyboard.nextInt();
        manual.setTotalNumberOfEntries(totalNumberOfEntries);
        Collections.addAll(lib, article,mediaResource,magazine,manual);
        System.out.println(lib.first());
        System.out.println(lib.last());
        System.out.println(lib);

    }

}

我想不通。

Exception in thread "main" java.lang.ClassCastException: class VirtualLibrary cannot be cast to class java.lang.Comparable (VirtualLibrary is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
    at java.base/java.util.TreeMap.compare(TreeMap.java:1291)
    at java.base/java.util.TreeMap.put(TreeMap.java:536)
    at java.base/java.util.TreeSet.add(TreeSet.java:255)
    at P5.main(P5.java:50)

【问题讨论】:

  • VirtualLibrary 没有实现 Comparable,那么你为什么认为你可以随意转换它?
  • VirtualLibrary 没有实现 Comparable。它不能用作TreeSet 的元素类型(除非您在构造函数中提供Comparator)。
  • 任何想法如何使用 getClass() 来显示书籍、文章等?现在我得到了所有东西的 VirtualLibrary。
  • @MauricePerry 只需在所有类中覆盖 toString() 并打印集合。
  • @PratapiHemantPatel 如果我理解正确,toString() 也必须被覆盖才能在打印集时获得任何有意义的东西?

标签: java


【解决方案1】:

TreeSet 对插入其中的数据进行排序。默认情况下,它不知道如何对 java 类进行排序。

因此,要么您需要在构造函数中提供Comparator,要么您与TreeSet 一起使用的类应该实现Comparable

// using comparator
SortedSet<ClassA> set = new TreeSet<>( new Comparator() {
    @Override
    public int compare(ClassA a, ClassA b) {
        // your implementation 
    }
});

// java 8 lambda
SortedSet<ClassA> set = new TreeSet<>( (a, b) -> {
   // your implementation
});

或者

class ClassA implements Comparable<ClassA> {

    ....
    @Override
    public int compareTo(ClassA other) {
        // your implementation
    }
}

【讨论】:

  • 任何想法如何使用 getClass() 来显示书籍、文章等?现在我得到了所有东西的 VirtualLibrary。
  • @Override public String toString() { return this.getClass()+" "+this.getTotalNumberOfEntries(); }
  • 我只接受虚拟课程
猜你喜欢
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
相关资源
最近更新 更多