排序的Arraylist以错误的顺序打印出来

时间:2013-10-06 21:20:52

标签: java sorting printing arraylist

我有温度和天气的Arraylist。我使用插入排序方法对arraylist进行了排序。当我打印出来的时候,它没有按照打印的方式打印出来: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);
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您的评论Collections.sort无论如何都是在打印行之后。您只需执行Collections.sort然后打印而不是制作新方法insertionSort