我有一个整数数组,我需要按升序排序。我需要使用compareTo
。
我有一个问题的数组排序。最大的数字首先出现在数组中;其余的按升序排序。
驱动: import java.util.Scanner;
public class Driver2 {
public static void main(String[] args) {
int prior,compareResult;
String task;
Task[] taskList = new Task[4];
int [] taskListPrior = new int[4];
Scanner scan = new Scanner(System.in);
System.out.print("Enter priority (between 1 & 10) for breakfast: ");
prior = scan.nextInt();
task = "eatBreakfast";
Task eatBreakfast = new Task(prior,task);
System.out.println("Breakfast priority is: " + eatBreakfast.getPriority());
taskList[0] = eatBreakfast;
taskListPrior[0] = eatBreakfast.getPriority();
System.out.print("Enter priority (between 1 & 10) for lunch: ");
prior = scan.nextInt();
task = "eatLunch";
Task eatLunch = new Task(prior,task);
System.out.println("Lunch priority is: " + eatLunch.getPriority());
taskList[1] = eatLunch;
taskListPrior[1] = eatLunch.getPriority();
System.out.print("Enter priority (between 1 & 10) for dinner: ");
prior = scan.nextInt();
task = "eatDinner";
Task eatDinner = new Task(prior,task);
System.out.println("Dinner priority is: " + eatDinner.getPriority());
taskList[2] = eatDinner;
taskListPrior[2] = eatDinner.getPriority();
System.out.print("Enter priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
task = "haveNap";
Task haveNap = new Task(prior,task);
System.out.println("Nap priority is: " + haveNap.getPriority());
taskList[3] = haveNap;
taskListPrior[3] = haveNap.getPriority();
System.out.println("--------------");
System.out.println("Printing task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}
System.out.print("Enter new priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
haveNap.setPriority(prior);
System.out.println("New priority for nap is: " + haveNap.getPriority());
taskList[3] = haveNap;
taskListPrior[3] = haveNap.getPriority();
System.out.println("--------------");
System.out.println("Printing task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}
Task tempTask;
int tempTaskPrior;
for(int current = 0 ; current < taskList.length ; current++)
{
for(int i = 1 ; i < (taskList.length) ; i++)
{
compareResult = (taskList[current]).compareTo(taskList[i]);
if(compareResult == 1){
tempTask = (taskList[i]);
tempTaskPrior = (taskListPrior[i]);
taskList[i] = (taskList[current]);
taskListPrior[i] = (taskListPrior[current]);
taskList[current] = tempTask;
taskListPrior[current] = tempTaskPrior;
}
}
}
System.out.println("Printing sorted list of task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}
}
}
任务类: public class Task实现Priority,Comparable { public int prior,priorityQuery,priorityNum; public String task,taskItem,fullTask; 任务taskName;
public Task(int priorityQuery, String taskQuery){
task = taskQuery;
if (priorityQuery >= minPriority && priorityQuery <= maxPriority)
prior = priorityQuery;
else
System.out.println("Out of valid range, task will have no priority");
}
public void setPriority(int priority)
{
if (priority >= minPriority && priority <= maxPriority)
prior = priority;
else
System.out.println("Out of valid range, task will have no priority");
}
public int getPriority()
{
return prior;
}
public String toString(){
taskItem = task;
return taskItem;
}
public Task getTaskName(){
return taskName;
}
public int compareTo(Task compTask){
if (this.getPriority() < compTask.getPriority())
return 1;
else if (this.getPriority() > compTask.getPriority())
return -1;
else
return 0;
}
}
实际上有两个并行数组,用于compareTo
方法的数组只包含整数。
答案 0 :(得分:0)
那个排序程序出了问题。
你每次从1开始内循环,current
可能是&gt;比i
。
因此,它(在某些情况下)会将到位的项目交换到不合适的位置。
正确答案是使用java.util.Arrays.sort()
。
如果您对排序算法感兴趣,请编写一个独立的类并实现单元测试脚本,以确保它在嵌入代码之前有效。
答案 1 :(得分:0)
正如我在评论中提到的那样,您应该尝试使用Arrays.sort(Object[] array)
:
public class Driver {
public static void main(String[] args) {
int prior, compareResult;
String task;
Task[] taskList = new Task[4];
Scanner scan = new Scanner(System.in);
System.out.print("Enter priority (between 1 & 10) for breakfast: ");
prior = scan.nextInt();
task = "eatBreakfast";
Task eatBreakfast = new Task(prior, task);
System.out.println("Breakfast priority is: " + eatBreakfast.getPriority());
taskList[0] = eatBreakfast;
System.out.print("Enter priority (between 1 & 10) for lunch: ");
prior = scan.nextInt();
task = "eatLunch";
Task eatLunch = new Task(prior, task);
System.out.println("Lunch priority is: " + eatLunch.getPriority());
taskList[1] = eatLunch;
System.out.print("Enter priority (between 1 & 10) for dinner: ");
prior = scan.nextInt();
task = "eatDinner";
Task eatDinner = new Task(prior, task);
System.out.println("Dinner priority is: " + eatDinner.getPriority());
taskList[2] = eatDinner;
System.out.print("Enter priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
task = "haveNap";
Task haveNap = new Task(prior, task);
System.out.println("Nap priority is: " + haveNap.getPriority());
taskList[3] = haveNap;
System.out.println("--------------");
System.out.println("Printing task names and priorities:");
for (Task t : taskList) {
System.out.println(t.prior + " - " + t.task);
}
System.out.print("Enter new priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
haveNap.setPriority(prior);
System.out.println("New priority for nap is: " + haveNap.getPriority());
taskList[3] = haveNap;
System.out.println("--------------");
System.out.println("Printing task names and priorities:");
for (Task t : taskList) {
System.out.println(t.prior + " - " + t.task);
}
// Sort this
Arrays.sort(taskList);
System.out.println("Printing sorted list of task names and priorities:");
for (Task t : taskList) {
System.out.println(t.prior + " - " + t.task);
}
}
}
我删除了对int [] taskListPrior = new int[4];
的所有引用,这些引用毫无意义,并用简单的Arrays.sort(taskList)
替换了您的排序算法。
您的类任务可能保持不变,但您应该扩展其compareTo方法,以便解决两个比较任务的优先级相同的情况。我在任务栏中添加了一个比较:
public int compareTo(Task compTask) {
if (compTask==null) return 1;
if (this.getPriority() < compTask.getPriority())
return 1;
else if (this.getPriority() > compTask.getPriority())
return -1;
else
return this.task.compareTo(compTask.task);
}