【问题标题】:'Can't find symbol' error in compare method for a Sorting program排序程序的比较方法中出现“找不到符号”错误
【发布时间】:2016-10-23 00:28:47
【问题描述】:

我有一个类任务,我们必须对包含 Integer、Double、Book 和 PlayingCard 对象的 ArrayList 进行排序。我们必须使用“instanceof”关键字和“compareTo”方法进行排序。我在使用名为“compare”的方法时遇到问题,该方法采用列表中的两个元素并检查它们是什么类型,然后如果它们是相同的类型,则使用 compareTo 方法对它们进行排序。整数和双精度数应该被视为相同类型,因为它们被排序在一起,我知道这会给 compareTo 方法带来问题,因为它只能比较相同类型的两个对象。每当我尝试使用通过的两个元素 (a.compareTo(b)) 调用 compareTo 方法时,我都会不断收到“找不到符号”错误。我已经尝试将它们从 Object 类型转换为它们各自的类类型......但无济于事。我只是不确定我在这里做错了什么。任何帮助是极大的赞赏。如果您需要查看 Book 或 PlayingCard 类的代码,我也可以提供。 (是的,Book 和 PlayingCard 都有自己的 compareTo 方法并且都实现了 Comparable)。

代码如下:

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&lt;Book&gt; 并有自己的public int compareTo(Book otherBook) 方法才能工作。
  • 感谢您的回复! Book 和 PlayingCard 都实现了 Comparable 并有自己的 compareTo 方法。
  • 不是一段,你能给我们实际的编译器输出吗?

标签: java instanceof compareto


【解决方案1】:

您正在对 Object 类的实例调用 compareTo。如果你查看类 Object (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 进行比较。 为此,您需要选择可以使用的 compareTo 方法,如果您研究转换和 int/double 的表示,那么您应该选择的方法是从 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());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    相关资源
    最近更新 更多