【发布时间】:2016-10-23 00:28:47
【问题描述】:
我有一个类任务,我们必须对包含 Integer、Double、Book 和 PlayingCard 对象的 ArrayList
代码如下:
import java.util.ArrayList;
public class Sorter {
/**
* Checks two objects in the list to see what type they are,
* if they are the same type, use compareTo method to sort,
* otherwise, sort in order of Integer and Double > Book > PlayingCard.
*
* @param a first object
* @param b second object
* @return a negative number if a < b, a positive number if a > b,
* 0 if a = b
*/
public static int compare(Object a, Object b) {
if ((a instanceof Integer || a instanceof Double) && (b instanceof Integer || b instanceof Double)) {
return a.compareTo(b);
}
else if ((a instanceof Integer || a instanceof Double) && (b instanceof Integer == false || b instanceof Double == false)) {
return -1;
}
else if ((a instanceof Integer == false || a instanceof Double == false) && (b instanceof Integer || b instanceof Double)) {
return 1;
}
else if ((a instanceof Book) && (b instanceof PlayingCard)) {
return -1;
}
else if ((a instanceof PlayingCard) && (b instanceof Book)) {
return 1;
}
else if ((a instanceof Book) && (b instanceof Book)) {
return a.compareTo(b);
}
else {
return a.compareTo(b);
}
}
/**
* Sort a list of objects. Uses the selection sort algorithm.
*
* @param stuff list of objects
*/
public static void sort(ArrayList<Object> stuff) {
// selection sort
for (int i = 0; i < stuff.size() - 1; i++) {
int lowest = i;
for (int j = 1; j < stuff.size(); j++) {
if (compare(stuff.get(j), stuff.get(lowest)) < 0) {
lowest = j;
}
}
// swap to front
if (lowest != i) {
Object temp = stuff.get(i);
stuff.set(i, stuff.get(lowest));
stuff.set(lowest, temp);
}
}
}
/**
* Main method. Populates an arraylist of stuff and sorts it.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
list.add(8);
list.add(new PlayingCard(PlayingCard.HEARTS, PlayingCard.TWO));
list.add(3.5);
list.add(new Book("Mark Twain", "The Adventures of Huckleberry Finn"));
list.add(new Book("F. Scott Fitzgerald", "The Great Gatsby"));
list.add(5.65);
list.add(new PlayingCard(PlayingCard.CLUBS, PlayingCard.SEVEN));
list.add(new PlayingCard(PlayingCard.SPADES, PlayingCard.ACE));
System.out.println("Original List: \n" + list); //debugging help
sort(list);
System.out.println("Sorted List: \n" + list);
}
}
这是编译器错误:
Sorter.java:16: error: cannot find symbol
return a.compareTo(b);
^
symbol: method compareTo(Object)
location: variable a of type Object
Sorter.java:31: error: cannot find symbol
return a.compareTo(b);
^
symbol: method compareTo(Object)
location: variable a of type Object
Sorter.java:34: error: cannot find symbol
return a.compareTo(b);
^
symbol: method compareTo(Object)
location: variable a of type Object
3 errors
PlayingCard 的 compareTo 方法:
public int compareTo(PlayingCard other) {
if (getSuit() < other.getSuit()) {
return -1;
}
else if (getSuit() > other.getSuit()) {
return 1;
}
else {
if (getRank() < other.getRank()) {
return -1;
}
else if (getRank() > other.getRank()) {
return 1;
}
else {
return 0;
}
}
}
图书的 compareTo 方法:
public int compareTo(Book other) {
if (getAuthor().compareTo(other.getAuthor()) < 0) {
return -1;
}
else if (getAuthor().compareTo(other.getAuthor()) > 0) {
return 1;
}
else {
if (getTitle().compareTo(other.getTitle()) < 0) {
return -1;
}
else if (getTitle().compareTo(other.getTitle()) > 0) {
return 1;
}
else {
return 0;
}
}
}
【问题讨论】:
-
图书需要实现
Comparable<Book>并有自己的public int compareTo(Book otherBook)方法才能工作。 -
感谢您的回复! Book 和 PlayingCard 都实现了 Comparable 并有自己的 compareTo 方法。
-
不是一段,你能给我们实际的编译器输出吗?
标签: java instanceof compareto