1

I am working on a program that will take a list of temperatures(double) and days(string) and implement the list using an array of objects. Then I need to sort the objects using an insertion sort algorithm. The output of the program should be the original order and the sorter output. However I am a little confused on how I can go about sorting the temperatures. I implemented the Comparable interface and wrote the insertion sort. I just have to have the original arraylist to print out and the sorted arraylist to print out. I wrote a toString method to print out the original and it compiles but does not print. here is my code:

 import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;


public class DailyTemperature implements Comparable<DailyTemperature>
{
     //variables
    private Double temperature;
    private String day;

    //getTemp & setTemp methods
    public double getTemp()
    {
      return temperature;
    }

    public void setTemp(double newTemp)
    {
      temperature = newTemp;
    }

    //getDay & setTEmp methods
    public String getDay()
    {
      return day;
    }

    public void setDay(String newDay)
    {
      day = newDay;
    }


    public DailyTemperature(String day, double temperature) 
    {
      this.day = day;
      this.temperature = temperature;
    }

    public int compareTo(DailyTemperature other) 
    {
        if (temperature < other.temperature) return -1;
        if (temperature == other.temperature) return 0;
        return 1;
    }

    public String toString() 
    {
        return("Day of Week" + this.getDay() +
        "Temperature" + this.getTemp());
    }


}





import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class DailyTemperatureList 
{
    public static void main (String [] args) 
    {

    ArrayList<DailyTemperature> dailytemps = new ArrayList<DailyTemperature>();

    dailytemps.add(new DailyTemperature("Mon", 87.1));
    dailytemps.add(new DailyTemperature("Tue", 88.3));
    dailytemps.add(new DailyTemperature("Wed", 81.2));
    dailytemps.add(new DailyTemperature("Thu", 84.0));
    dailytemps.add(new DailyTemperature("Fri", 76.3));
   }

   public static <T extends Comparable<? super T>>
   void insertionSort(ArrayList<DailyTemperature> dailytemps)
   {
       DailyTemperature temp = null;
       int position = 0;

       //loop from 2nd element on
       for (int i = 1; i < dailytemps.size(); i++)
       {
           temp = dailytemps.get(i);
           position = i;

           while ( 0 < position && temp.compareTo(dailytemps.get(position - 1 )) < 0)
           {
               dailytemps.set(position, dailytemps.get(position - 1));
               position--;
           }
            dailytemps.set(position,temp);
        }
        System.out.println( dailytemps.toString() );
    }




}
4

2 回答 2

0

@dkatzel 很到位。我只想补充一点,您可以通过使您的compareTo实现更紧密来充分利用 Java 提供的开箱即用功能:

为简洁起见,这里是同一个类:

public class DailyTemperature implements Comparable<DailyTemperature>
{
     //variables
    private Double temperature;

    public int compareTo(DailyTemperature other) {
        return temperature.compareTo(other.temperature);
    }

}

如果您在内部将温度存储为 aDouble并利用自动装箱(它可以让您无缝地从double<-> Double...ish),那么您可以利用事实Double实现并将自定义类中Comparable的实现委托给's.compareToDouble

然后你完全按照@dkatzel 所说的那样进行插入排序,temperature.compareTo调用替换了通常的原始比较。

巧合的是,我们刚刚完成了一个关于 Java 比较的教程。如果您想对它们的工作原理进行更多解释,请查看。

希望有帮助。

于 2013-10-06T15:30:18.027 回答
0

您需要添加public int compareTo(DailyTemperature)Comparable 接口所需的方法

public class DailyTemperature implements Comparable<DailyTemperature>{

   //...

   public int compareTo(DailyTemperature other){

      //your code goes here if "this"< than other, return a negative int
      //if this > other return positive int
      //if they are equal in the eyes of sort, then return 0

   }

}

编辑:你的排序会使用这样的比较

 DailyTemperature a = ...
 DailyTemperature b = ...

 if(a.compareTo(b) < 0){
     // a < b
 }else{
     // a >=b

 }
于 2013-10-06T03:05:37.747 回答