2015-10-22 99 views
0

嗨,夥計們我正在學習或試圖學習如何使用compareTo()..我有一個簡單的類實現它,但在相反的順序,我想有人可以請指出明顯對我來說,因爲我似乎一直想念它。我如何反轉這一點。 這是我的簡單例子。學習使用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(); 
    } 


} 

任何意見將是真棒......謝謝

+0

顛倒什麼順序? – Luminous

+1

您可以通過將'compareTo'減少爲單行來簡化和反轉順序:'return other.getPriority() - getPriority()' – neuronaut

+0

謝謝..非常簡單的好建議。 – Brett

回答

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()。所以你可以讓它返回你喜歡的任何東西。