-1

我有一个温度和天数的数组列表。我使用插入排序方法对数组列表进行了排序。当我将其打印出来时,它不会以排序方式打印出来:76、84、81、88、87。可能是因为我注释掉了 Collections.sort(dailytemps) 行吗?它出错了,所以我把它注释掉了

这是我的代码:

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

public class DailyTemperature 
    {

    //variables
    private double temperature;
    private String day;

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

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

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

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

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

  //compareTo method used for Comparable interface
  public int compareTo(DailyTemperature other)
  {
    if (temperature < other.temperature) return -1;
    if (temperature == other.temperature) return 0;
    return -1;
  }


  //toString method to print out original arraylist contents and sorted arraylist contentes
  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));

    //original list printout
    System.out.println("Original List:" );
    System.out.println( dailytemps.toString() );
    System.out.println(" "); //empty line

    //method to sort array list
    insertionSort(dailytemps);
    //sorted list printout
    System.out.println("Sorted List:" );
    System.out.println( dailytemps.toString() );

    //Collections.sort(dailytemps);
  }

  static 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);
    }
  }
}
4

2 回答 2

2

您的compareTo()实现不正确。如果this.temperature大于 ,则永远不会返回 1 other.temparature

  public int compareTo(DailyTemperature other)
  {
    if (temperature < other.temperature) return -1;
    if (temperature == other.temperature) return 0;
    return -1; // return 1; here
  }
于 2013-10-06T21:24:34.560 回答
1

无论如何,您的评论Collections.sort都是在打印行之后。您可以简单地做Collections.sort然后打印而不是创建一个新方法insertionSort

于 2013-10-06T21:27:22.797 回答