学习使用compareTo

时间:2015-10-22 17:55:16

标签: java

嗨大家我正在学习或试图学习如何使用compareTo()..我有一个简单的类来实现它,但是在相反的顺序中,我想有人可以指出显而易见的对我来说,因为我似乎总是想念它。我该如何扭转这种局面。 这是我的简单例子。

import java.util.*;

    public class H283
    {
      public static void main(String args[])
      {
        Task[] tasks = new Task[4];
        tasks[0] = new Task("Homework");
        tasks[0].setPriority(7);
        tasks[1] = new Task("Eat lunch");
        tasks[1].setPriority(Priority.MIN_PRIORITY);
        tasks[2] = new Task("Attend class");
        tasks[2].setPriority(Priority.MAX_PRIORITY);
        tasks[3] = new Task("Ned's project");
        tasks[3].setPriority(4);
        Arrays.sort(tasks);
        System.out.println("\n  TO-DO\n---------");
        for (int i = 0; i < tasks.length; i++)
          System.out.println(tasks[i].getName() + " \tpriority: " + tasks[i].getPriority());

      }
    }

    interface Priority
    {
      static final int MED_PRIORITY = 5;
      static final int MAX_PRIORITY = 10;
      static final int MIN_PRIORITY = 1;

      //-----------------------------------------------------------------
      //  Sets the object's priority level.
      //-----------------------------------------------------------------
      public void setPriority (int value);

      //-----------------------------------------------------------------
      //  Returns the object's priority level.
      //-----------------------------------------------------------------
      public int getPriority();
    }

    class Task implements Priority, Comparable<Task>
    {
      String name;
      private int priority;

      //-----------------------------------------------------------------
      //  Sets up this task with a medium priority level.
      //-----------------------------------------------------------------
      public Task (String taskName)
      {
        name = taskName;
        priority = MED_PRIORITY;
      }

      //-----------------------------------------------------------------
      // Returns this task's name.
      //-----------------------------------------------------------------
      String getName()
      {
        return name;
      }

      //-----------------------------------------------------------------
      //  Sets this task's priority level.
      //-----------------------------------------------------------------
      public void setPriority (int value)
      {
        priority = value;
      }

      //-----------------------------------------------------------------
      //  Returns this task's priority level.
      //-----------------------------------------------------------------
      public int getPriority()
      {
        return priority;
      }


      public int compareTo( Task other )
      {
//here is where i'd like to reverse the order


 int thePriority = getPriority()-other.getPriority();
    if ( thePriority != 0 )
      return thePriority;
    else
      return getPriority()-other.getPriority();
  }


}

任何建议都很棒......谢谢

1 个答案:

答案 0 :(得分:0)

public int compareTo( Task other ) {
    //here is where i'd like to reverse the order
    // Simply reverse the order of your subtraction
    int thePriority = other.getPriority()-getPriority();
        if ( thePriority != 0 )
           return thePriority;
        else
           return getPriority()-other.getPriority();
}

只需颠倒减法的顺序就是一种选择。或者你可以确切地保留你拥有的东西并且只是否定它。

public int compareTo( Task other ) {
    //here is where i'd like to reverse the order
    int thePriority = getPriority()-other.getPriority();
        if ( thePriority != 0 )
           return -thePriority; // Return negated priority
        else
           return getPriority()-other.getPriority();
}

此外,您的其他声明目前返回与您的if完全相同的内容。除非你想要处理其他同样优先级排序任务的方法(例如,按名称排序),否则你可以轻松地省去整个if / else语句并让compareTo()方法成为一行。

public int compareTo( Task other ) { // Clean and simple!
    return -(getPriority()-other.getPriority());
}

在这种情况下,当你实现自己的compareTo()时,你并没有那么多了解compareTo()。因此,您可以随意返回。

相关问题