Exception in thread "main" java.lang.ClassCastException: com.alpha.Book cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at com.alpha.MainClass.main(MainClass.java:25)
public interface Comparable<T> {
int compareTo(T o);
}
package com.alpha;
import java.util.Arrays;
class Book implements Comparable<Book> {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "Book [title=" + title + ", price=" + price + "]\n";
}
@Override
public int compareTo(Book o) { // Arrays.sort()会自动调用此方法
if (this.price > o.price) {
return 1;
} else if (this.price < o.price) {
return -1;
}
return 0;
}
}
public class MainClass{
public static void main(String[] args) throws Exception {
Book[] books = new Book[] {
new Book("C++", 15.6),
new Book("Java", 29.8),
new Book("Python", 9.9),
new Book("C#", 35.6)
};
Arrays.sort(books); // 对象数组排序
System.out.println(Arrays.toString(books));
}
}
class Book{
private String title;
private double price;
public Book() {}
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [title=" + title + ", price=" + price + "]\n";
}
}
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
class BookComparator implements Comparator<Book> {
@Override
public int compare(Book o1, Book o2) {
if (o1.getPrice() > o2.getPrice()) {
return 1;
} else if (o1.getPrice() < o2.getPrice()) {
return -1;
}
return 0;
}
}
public class MainClass{
public static void main(String[] args) throws Exception {
Book[] books = new Book[] {
new Book("C++", 15.6),
new Book("Java", 29.8),
new Book("Python", 9.9),
new Book("C#", 35.6)
};
Arrays.sort(books, new BookComparator());
System.out.println(Arrays.toString(books));
}
}
class Book implements Comparable<Book> {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "Book [title=" + title + ", price=" + price + "]\n";
}
@Override
public int compareTo(Book o) { // Arrays.sort()会自动调用此方法
if (this.price > o.price) {
return 1;
} else if (this.price < o.price) {
return -1;
}
return 0;
}
}
@SuppressWarnings("rawtypes")
class BinaryTree {
private class Node {
private Comparable data; // 排序的依据就是Comparable
private Node left; // 左子树
private Node rigth; // 右子树
public Node(Comparable data) {
this.data = data;
}
@SuppressWarnings("unchecked")
public void addNode(Node newNode) {
if (this.data.compareTo(newNode.data) > 0) {
if (this.left == null) {
this.left = newNode;
} else {
this.left.addNode(newNode);
}
} else {
if (this.rigth == null) {
this.rigth = newNode;
} else {
this.rigth.addNode(newNode);
}
}
}
public void toArrayNode() {
if (this.left != null) { // 表示有左子树
this.left.toArrayNode();
}
BinaryTree.this.retData[BinaryTree.this.foot ++] = this.data;
if (this.rigth != null) { // 表示有右子树
this.rigth.toArrayNode();
}
}
}
private Node root; // 定义根节点
private int count; // 保存元素个数
private Object[] retData;
private int foot;
public void add(Object obj) { // 进行数据的追加
Comparable com = (Comparable) obj; // 必须变为Comparable才可以实现Node
Node newNode = new Node(com); // 创建新的节点
if (this.root == null) { // 现在不存在根节点
this.root = newNode; // 保存根节点
} else {
this.root.addNode(newNode); // 交给Node类处理
}
this.count ++;
}
public Object[] toArray() {
if (this.root == null) {
return null;
}
this.foot = 0;
this.retData = new Object[this.count];
this.root.toArrayNode();
return this.retData;
}
}
public class MainClass{
public static void main(String[] args) throws Exception {
BinaryTree bt = new BinaryTree();
bt.add(new Book("C++", 15.6));
bt.add(new Book("Java", 29.8));
bt.add(new Book("Python", 9.9));
bt.add(new Book("C#", 35.6));
Object[] objs = bt.toArray();
System.out.println(Arrays.toString(objs));
}
}