1

我不擅长比较器,但我需要学习它,到目前为止我已经做了很多,但我在 'System.out.println(compy("January", "March", "October", " April"));'...请帮忙,我想要一个修复的解释。谢谢你!

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

        System.out.println(Comp("January","May"));
        System.out.println(Comp("October", "May"));
        System.out.println(Comp("August","August"));
        System.out.println(Comp("January", "March"));

        System.out.println(compy("January", "March", "October", "April"));
    }

    static int Comp(String s1, String s2){

        while (true){
            int r = 0;
            int s = 0;

            if(s1.equals("January")){
                r=1;
            }

            if(s1.equals("February")){
                r=2;
            }

            if(s1.equals("March")){
                r=3;
            }

            if(s1.equals("April")){
                r=4;
            }

            if(s1.equals("May")){
                r=5;
            }

            if(s1.equals("June")){
                r=6;
            }

            if(s1.equals("July")){
                r=7;
            } 

            if(s1.equals("August")){
                r=8;
            }

            if(s1.equals("September")){
                r=9;
            }

            if(s1.equals("October")){
                r=10;
            }

            if(s1.equals("November")){
                r=11;
            }

            if(s1.equals("December")){
                r=12;
            }

            if(s2.equals("January")){
                s=1;
            }

            if(s2.equals("February")){
                s=2;
            }

            if(s2.equals("March")){
                s=3;
            }

            if(s2.equals("April")){
                s=4;
            }

            if(s2.equals("May")){
                s=5;
            }

            if(s2.equals("June")){
                s=6;
            }

            if(s2.equals("July")){
                s=7;
            }

            if(s2.equals("August")){
                s=8;
            }

            if(s2.equals("September")){
                s=9;
            }

            if(s2.equals("October")){
                s=10;
            }

            if(s2.equals("November")){
                s=11;
            }

            if(s2.equals("December")){
                s=12;
            }

            if(r<s){
                return -1;
            }

            if(r>s){
                return 1;
            }
            if(r==s){
                return 0;
            }

        }   

    }

    public class Comp implements Comparator {
        public int compare(Object o1, Object o2){
            return Comp((String)o1, (String)o2);
        }
    }

    void sort(List l){
        Comparator compy = new Comp();
        Collections.sort(l, compy);
    }

}
4

3 回答 3

3

这是您可以使用代码对列表进行排序的方式。

List<String> list = new ArrayList<String>();

list.add("March");
list.add("January");
list.add("October");
list.add("April");

System.out.println("Before sort: " + list);
// prints: Before sort: [March, January, October, April]

new Months().sort(list); // sort

System.out.println("After sort: " + list);
// prints: After sort: [January, March, April, October]
于 2013-08-19T21:55:52.197 回答
2

Ravi Thapliyal 为您提供了很好的解决方案,但如果我可以提出建议,请更改您的比较器

public class Comp implements Comparator<String> {

   private List<String> months = Arrays.asList("January" , "February",...);

    @Override
    public int compare(String s1, String s2) {
        return months.indexOf(s1)-months.indexOf(s2);
    }
}

它更短,更易读

于 2013-08-19T22:09:17.203 回答
0

包括实际错误会有所帮助。

在我看来,错误是您在排序方法中使用了一个变量。从主要方法。

尝试类似的东西

package com.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class HomeWork {

  public static void main(String args[]) {
    MonthComparable compy = new MonthComparable();
    List<String> myList = Arrays.asList(new String[] {"May", "June", "April"});
    Collections.sort(myList, compy);
    System.out.println();
  }

  public static class MonthComparable implements Comparator<String> {

    @Override
    public int compare(String left, String right) {
      Integer leftValue = monthValue(left);
      Integer rightValue = monthValue(right);
      return leftValue.compareTo(rightValue);
    }

    private static int monthValue(String left) {
      // TODO Implement this
      return 0;
    }
  }
}
于 2013-08-19T21:46:27.307 回答