簡體   English   中英

如何使用單獨的比較器類對數組列表進行排序

[英]How to use a separate comparator class to sort an array list

是的,這是家庭作業,但是我非常想要的東西。 比較器類應實現java.util.Comparator,sort()方法的第二個參數應為我的Comparator類的實例。

到目前為止,我的比較器類如下所示:

import java.util.Comparator;

public class Compare implements Comparator<Rational> {

    public int compare1(int a, int b){
        int z = 0;

        if(a > b)
            z = 1;
        else if(b > a)
            z = -1;
        return z;
    }

    @Override
    public int compare(Rational a, Rational b) {
        // TODO Auto-generated method stub
        return 0;
    }
}

我有一個單獨的方法來對數組進行排序,但是此方法使用compareTo()

public static Collection<Rational> sort1(List<Rational> list){
    List<Rational> sortedList1 = new ArrayList<Rational>();

    for (int i=0;i < list.size();i++) { //iterating through list
        Rational currentValue = list.get(i);

        int pos = sortedList1.size(); 
        for (int j=0;j<sortedList1.size();j++) {
            int comparison = currentValue.compareTo(sortedList1.get(j)); //comparing
            //this is the right position if the currentValue is greater or equal 
            //to the sorted value at this position
            if(comparison > 0 || comparison == 0){
                pos = j;
                break;
            }
        }
        sortedList1.add(pos, currentValue);
    }
    return sortedList1;
}

除了單獨的比較器類,我應該使用什么方法? 我完全不知道該怎么辦。

我的Rational Class看起來像:

公共類Rational實現Comparable {

    private int num;   // the numerator
    private int den;   // the denominator

    // create and initialize a new Rational object
    public Rational(int numerator, int denominator) {
        if (denominator == 0) {
            throw new RuntimeException("Denominator is zero");
        }
        int g = gcd(numerator, denominator);
        num = numerator / g;
        den = denominator / g;

    }

    // return string representation of (this)
    public String toString() {
        if (den == 1) {
            return num + "";
        } else {
            return num + "/" + den;
        }
    }

    // return (this * b)
    public Rational times(Rational b) {
        return new Rational(this.num * b.num, this.den * b.den);
    }

    // return (this + b)
    public Rational plus(Rational b) {
        int numerator = (this.num * b.den) + (this.den * b.num);
        int denominator = this.den * b.den;
        return new Rational(numerator, denominator);
    }

    // return (1 / this)
    public Rational reciprocal() {
        return new Rational(den, num);
    }

    // return (this / b)
    public Rational divides(Rational b) {
        return this.times(b.reciprocal());
    }

    /** ***********************************************************************
     * Helper functions
    ************************************************************************ */
    // return gcd(m, n)
    private static int gcd(int m, int n) {
        if (0 == n) {
            return m;
        } else {
            return gcd(n, m % n);
        }
    }

    /** ***********************************************************************
     * Test client
    ************************************************************************ */
    public static void main(String[] args) {
        Rational x, y, z;

        // 1/2 + 1/3 = 5/6
        x = new Rational(1, 2);
        y = new Rational(1, 3);
        z = x.plus(y);
        System.out.println(z);

        // 8/9 + 1/9 = 1
        x = new Rational(8, 9);
        y = new Rational(1, 9);
        z = x.plus(y);
        System.out.println(z);

        //  4/17 * 7/3 = 28/51
        x = new Rational(4, 17);
        y = new Rational(7, 3);
        z = x.times(y);
        System.out.println(z);

        // 203/16957 * 9299/5887 = 17/899
        x = new Rational(203, 16957);
        y = new Rational(9299, 5887);
        z = x.times(y);
        System.out.println(z);

        // 0/6 = 0
        x = new Rational(0, 6);
        System.out.println(x);

    }

    public int compareTo(final Rational a, final Rational b) {
        return b.compareTo(a);
    }

    @Override
    public int compareTo(Rational arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
}

我會用

Collections.sort(list, RationalComparator.INSTANCE);

比較器的外觀

enum RationalComparator implements Comparator<Rational> {
   INSTANCE;

   public int compare(Rational a, Rational b) {
       // do your comparison here
   }
}
public class MyCompare implements Comparator<Rational> {

@Override
public int compare(final Rational a, final Rational b) {
    return a.compareTo(b);
}
}

接着:

Collections.sort(list, new MyCompare());

您可以自己回答:“並且sort()方法的第二個參數應該是我的Comparator類的實例。” 所以第一行變成

public static Collection<Rational> sort1(List<Rational> list,final Comparator<Rational,Rational> comparator){

此參數可用於您的方法中進行比較。

comparator.compare(...)

嘗試這個:

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

class Rational{

private static int gcd(int m, int n) {
    if (0 == n) return m;
    else return gcd(n, m % n);
}

private int num;   // the numerator
private int den;   // the denominator


public int getNum() {
    return num;
}



public void setNum(int num) {
    this.num = num;
}



public int getDen() {
    return den;
}



public void setDen(int den) {
    this.den = den;
}



// create and initialize a new Rational object
public Rational(int numerator, int denominator) {
    if (denominator == 0) {
        throw new RuntimeException("Denominator is zero");
}

int g = Rational.gcd(numerator, denominator);
num = numerator   / g;
den = denominator / g;

}



// return string representation of (this)
public String toString() {
if (den == 1) { return num + "";        }
else          { return num + "/" + den; }
}

// return (this * b)
public Rational times(Rational b) {
return new Rational(this.num * b.num, this.den * b.den);
}


// return (this + b)
public Rational plus(Rational b) {
int numerator   = (this.num * b.den) + (this.den * b.num);
int denominator = this.den * b.den;
return new Rational(numerator, denominator);
}

// return (1 / this)
public Rational reciprocal() { return new Rational(den, num);  }

// return (this / b)
public Rational divides(Rational b) {
    return this.times(b.reciprocal());

}


}


class RationalComparator implements Comparator<Rational>{

@Override
public int compare(Rational r1, Rational r2) {
    if (r1.getNum() / r1.getDen() == r2.getNum() / r2.getDen()){
        return (0);
        }
        else if (r1.getNum() / r1.getDen() < r2.getNum() / r2.getDen()){
        return (-1);
        }
        else{
        return (1);


        }

    }

}

 class UsingComparator {

    public static void main(String[] args) {

        Rational r1=new Rational(5, 2);
        Rational r2=new Rational(1, 2);

        List<Rational>rationalList=new ArrayList<Rational>();

        rationalList.add(r1);
        rationalList.add(r2);
        System.out.println(rationalList);

        Collections.sort(rationalList,new RationalComparator());
        System.out.println(rationalList);


    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM