简体   繁体   English

使用Java中的插入排序算法对数组列表中的温度进行排序

[英]Sorting temperatures in a array list using insertion sort algorithm in java

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. 我实现了Comparable接口并编写了插入排序。 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. 我写了一个toString方法来打印出原始的,它可以编译但不能打印。 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() );
    }




}

You need to add the method public int compareTo(DailyTemperature) that is required for the Comparable interface 您需要添加Comparable接口所需的方法public int compareTo(DailyTemperature)

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

   }

} }

EDIT: your sort would use the comparables like this 编辑:您的排序将使用类似的可比对象

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

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

 }

@dkatzel is spot on. @dkatzel被发现。 I would only add that you can take some more advantage of the facilities provided by Java out of the box by making your compareTo implementation tighter: 我只补充说,通过使您的compareTo实现更加严格,您可以利用Java提供的更多功能:

Here is the same class edited for brevity: 这是为简洁起见编辑的同一类:

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

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

}

If you store temperature internally as a Double and take advantage of autoboxing (which lets you go from double <-> Double seamlessly...ish), you can then take advantage of the fact Double implements Comparable and delegate the compareTo implementation in your custom class to Double 's. 如果您在内部将温度存储为Double并利用自动装箱(这使您可以从double <-> Double无缝实现... ish),则可以利用Double实现Comparable并在自定义中委派compareTo实现这一事实类为Double的。

Then you do your insertion sort exactly as @dkatzel said, with the temperature.compareTo calls replacing the usual primitive comparisons. 然后,您按照@dkatzel所说的那样进行插入排序,使用temperature.compareTo调用替换通常的原始比较。

Coincidentally, we just finished a tutorial on comparisons in Java. 巧合的是,我们刚刚完成了有关Java比较的教程 Have a look if you want some more explanation on how they work. 看看是否需要更多有关它们如何工作的解释。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM