Can you implement and override the list.sort() met

2019-08-11 06:33发布

问题:

So I've been given the following problem:

Write a program that creates a List of Rationals and sorts them into increasing order. Use appropriate methods from the Collections Framework classes to sort the elements into increasing order.

I've created a 'Rational' class to represent rational numbers and I've also made the list of random Rational numbers. But I'm having trouble figuring out a way to implement a method of sorting the list. Here's samples of the code before I go on further:

public class Rational implements Comparable<Rational> {
private int num;
private int denom;
private int common;

// Default constructor initialises fields
public Rational() throws IllegalNumDenomException {
    setNum(1);
    setDenom(2);
}

// Constructor sets fields with given parameters
public Rational(int num, int denom) throws IllegalNumDenomException {
    common = gcd(num,denom);
    setNum(num/common);
    setDenom(denom/common);
}

//Compares two rational numbers
public int compareTo(Rational rhs) {
    int tempNumerator = this.getNum() * rhs.getDenom();
    int tempNumeratorRhs = rhs.getNum() * this.getDenom();

    //Compares rationalised numerators and returns a corresponding value
    if (tempNumerator < tempNumeratorRhs) {
        return -1;
    } else if (tempNumerator > tempNumeratorRhs) {
        return 1;
    }
    return 0;
}

// Overriden toString method
public String toString() {
    return num + "/" + denom;
}

//Calculates the GCD of a fraction to simplify it later on
public int gcd(int x, int y) throws IllegalNumDenomException{
    while(x != 1){ //Prevents infinite loop as everything is divisible by 1
        if(x == y){
            return x;
        }
        else if(x>y){
            return gcd(x-y,y);
        }
        return gcd(x,y/x);
    }
    return 1;
}

public class RationalList {

public static void main(String[] args) throws IllegalNumDenomException {
    List<Rational> rationals = new ArrayList<Rational>();
    Random rand = new Random();
    int n = rand.nextInt(50) + 1;

    //Generates 9 random Rationals
    for(int i = 1; i<10; i++){
        rationals.add(new Rational(i,n));
        n = rand.nextInt(50) + 1;
    }

    System.out.println("Original Order: " + rationals.toString());
    sort(rationals);
    System.out.println(rationals);
}

public static List<Rational> sort(List<Rational> rationals){
    //Use compareTo method inside a loop until list is sorted

    return rationals;
}

Sorry it's a bit long. So my thinking is creating a sort method and using the compareTo method to determine if a Rational is in the correct place, if not swap it. But then I'm unsure if you're able to even move elements around in a list like you can in an array. So I then thought maybe I need to implement the Collections.sort() method and override the sort method but I get to the same problem. Maybe I could use .toArray?

Can anyone shed some light on the way to do this please? Just hints would be useful.

回答1:

Since you implemented comparable, Collections.sort(rationals) will work.

This is because Collections.sort will work on any List of Comparable things. It has already been designed to use the Comparable.compareTo() method that you have defined, and as long as your compareTo is implemented correctly it should sort your list.



回答2:

What you are doing is roughly correct.

But then I'm unsure if you're able to even move elements around in a list like you can in an array.

Under the hood, the Collections.sort method can copy the elements of the list into an array, sort the array, and then rebuild the list from the sorted array. The actual behavior depends on the list implementation class.



回答3:

In the main method of your application you should create a list of Rationals and then use the Collections.sort() method.

You should generate the random list of Rationals and then use Collection.sort(rationalsList);