我有一个类的任务,在这个任务中,我们必须对包含整数、双精度、ArrayList<和PlayingCard对象的Object>进行排序。我们必须使用'instanceof‘关键字和'compareTo’方法进行排序。我在使用名为"compare“的方法时遇到了问题,该方法接受列表中的两个元素,并检查它们的类型,然后如果它们是相同的类型,则使用compareTo方法对它们进行排序。整数和双精度数应该被视为相同的类型,因为它们排序在一起,我知道这会给compareTo方法带来问题,因为它只能比较两个相同类型的对象。每当我尝试使用传递的两个元素(a.compareTo(b))调用compareTo方法时,我都会收到“无法找到符号”的错误。我已经尝试将它们从对象类型转换为各自的类types...to,但都无济于事。我只是不确定我到底做错了什么。任何帮助都是非常感谢的。如果您需要查看Book或PlayingCard类的代码,我也可以提供。(是的,Book和PlayingCard都有自己的compareTo方法,并且都实现了可比较的方法)。
代码如下:
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 errorsPlayingCard的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;
}
}
}Book的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;
}
}
}发布于 2016-10-23 09:01:33
您正在对类Object的实例调用compareTo。如果你查找类对象(https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html),你会发现它没有这样的方法。这就是编译器告诉你的,尽管它使用“符号”这个词来表示方法或字段。
在调用compareTo之前,需要将对象强制转换为Integer或Double的实例,如下面的代码所示,然后可以使用Integer的compareTo方法或Double的compareTo方法,具体取决于您拥有的内容:
if(a instanceof Double && b instanceof Double) {
Double aDouble = (Double) a;
Double bDouble = (Double) b;
return aDouble.compareTo(b);
}在本例中,最有可能的情况是,本练习希望您将Integer与Double进行比较。为此,您需要选择您可以使用的整型方法,如果您查看转换和整型/双精度型的表示,那么您应该选择从整型到双精度型,因此您将需要以下内容作为选项之一:
if(a instanceof Integer && b instanceof Double) {
Integer aInteger = (Integer) a;
Double bDouble = (Double) b;
return -bDouble.compareTo(aInteger.doubleValue()); //note the negative
}这是另一种选择:
if(a instanceof Double && b instanceof Integer ) {
Double aDouble = (Double) a;
Integer bInteger = (Integer ) b;
return aDouble.compareTo(bInteger.doubleValue());
}https://stackoverflow.com/questions/40198413
复制相似问题