为什么排序方法不起作用?

时间:2015-11-09 02:33:12

标签: java

我必须在这里对一个分数数组进行排序是我的代码,这个代码工作正常。

public class Fraction implements Comparable<Fraction>{
    private int numerator;
    private int denominator;

    public Fraction(int num, int den){
        numerator = num;
        denominator = den;
    }

    public int compareTo(Fraction fraction){
        if(decimalValue()>fraction.decimalValue()){
            return 1;
        }else if(decimalValue()<fraction.decimalValue()){
            return -1;
        }else{
            return 0;
        }
    }
    public Fraction reduce(int numerator, int denominator){

  if(numerator==0&&denominator==0){
     numerator = 0;
     denominator = 0;
  }
  else{
     for(int x = Math.min(Math.abs(numerator), Math.abs(denominator)); x>0; x--){
        if(denominator == numerator){
           numerator = 1;
           denominator = 1;
        }
        else if(numerator == 0){
           numerator = 0;
           denominator = 1;
        }
        else if(numerator%x==0 && denominator%x==0){
           numerator = numerator/x;
           denominator = denominator/x; 
        }
     }
  }

    public double decimalValue(){
        double decimal = (numerator*1.0)/(1.0*denominator);
        return decimal;
    } 
    public String toString(){
        reduce(numerator, denominator);
        return ((numerator) + "/" + (denominator));
    }
}

由于某些原因,如果我将它与比较器一起使用,那么sort()就不起作用了,但是我不明白为什么它不起作用。这是测试人员:

public class FractionChecker{
    public static void main (String[]args){
        int n, d;
        Random rand = new Random();
        Fraction[] f = new Fraction [20];
        for (int j= 0; j<20; j++){
            n = rand.nextInt(20);
            d = rand.nextInt(19)+1;           
            f[j] = new Fraction (n,d);
        }

        System.out.println("Unsorted " + Arrays.toString(f));
        Arrays.sort(f);
    }
}

错误:

      ----jGRASP exec: java FractionChecker
      Unsorted [7/6, 15/14, 5/15, 8/9, 19/16, 16/5, 11/16, 2/9, 11/10, 10/12,       12/11, 9/18, 15/4, 11/4, 10/7, 12/8, 13/14, 19/5, 19/15, 13/5]
       Exception in thread "main" java.lang.ClassCastException: Fraction cannot be cast to java.lang.Comparable
      at java.util.Arrays.mergeSort(Arrays.java:1144)
      at java.util.Arrays.mergeSort(Arrays.java:1155)
      at java.util.Arrays.mergeSort(Arrays.java:1155)
      at java.util.Arrays.sort(Arrays.java:1079)
      at FractionChecker.main(FractionChecker.java:18)

     ----jGRASP wedge: exit code for process is 1.
     ----jGRASP: operation complete.

This is the error I am getting when I use Arrays.sort(f) and I am not sure why.

3 个答案:

答案 0 :(得分:1)

Collections.sort期望List类型实现Comparable。您将提供一系列Fraction对象。

您应该使用Arrays.sort代替:

Arrays.sort(f);

如果由于某些原因上述情况引发了ClassCastException,您可以尝试使用此版本的Arrays.sort,这需要Comparator作为参数进行比较:

Arrays.sort(f, new java.util.Comparator<Fraction>() {
    @Override
    public int compare(Fraction f1, Fraction f2) {
        return f1.compareTo(f2);
    }
});

答案 1 :(得分:0)

我没有看到您的this.fun = function( ) { myFunction(); } 代码。在任何情况下比较分数的更好方法可能是:

decimalValue

并不是说这会大大提高你的表现,但它只是整洁,并以合理的方式处理除零。

答案 2 :(得分:0)

以下代码适用并打印success

import java.util.Arrays;

public class Fraction implements Comparable<Fraction>{
    private int numerator;
    private int denominator;

    public Fraction(int num, int den){
        numerator = num;
        denominator = den;
    }

    public int compareTo(Fraction fraction){
        if(decimalValue()>fraction.decimalValue()){
            return 1;
        }else if(decimalValue()<fraction.decimalValue()){
            return -1;
        }else{
            return 0;
        }
    }

    public double decimalValue(){
        double decimal = (numerator*1.0)/(1.0*denominator);
        return decimal;
    } 
    public String toString(){
        return ((numerator) + "/" + (denominator));
    }

    public static void main(String[] a) {
        Fraction[] fractions = new Fraction[2];
        fractions[0] = new Fraction(1,1);
        fractions[1] = new Fraction(2,3);
        Arrays.sort(fractions);
        System.out.println("success");
    }
}