gpt4 book ai didi

java - TreeSet 的类转换异常问题

转载 作者:行者123 更新时间:2023-12-04 00:27:00 28 4
gpt4 key购买 nike

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

创建一个名为 VirtualLibrary 的泛型类具有单个属性 totalNumberOfEntries , 以及使用户能够设置和返回条目的方法。条目类型为 Book , Article , MediaResource , MagazineManual .为每种类型的条目实现特定的类。在 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)

最佳答案

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
}
}

关于java - TreeSet 的类转换异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745743/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com