大家好,我正在学习或尝试学习如何使用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();
}
}任何建议都是很棒的。谢谢
发布于 2015-10-23 02:27:44
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();
}另外,您的else语句当前返回的内容与if完全相同。除非您希望以其他方式对优先级相同的任务进行排序(例如,按名称排序),否则您可以很容易地省略整个if/else语句,并使compareTo()方法只占一行。
public int compareTo( Task other ) { // Clean and simple!
return -(getPriority()-other.getPriority());
}在这种情况下,与其说是在学习compareTo(),不如说是在实现自己的compareTo()。所以你可以让它返回你喜欢的任何东西。
https://stackoverflow.com/questions/33287486
复制相似问题