您可以实现并重写list.sort()方法以对有理数列表进行排序吗? [英] Can you implement and override the list.sort() method to sort a list of rational numbers?

查看:184
本文介绍了您可以实现并重写list.sort()方法以对有理数列表进行排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了以下问题:

So I've been given the following problem:

编写一个程序,该程序创建一个Rational列表并将其排序为递增 命令.使用Collections Framework类中的适当方法进行排序 元素按升序排列.

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;
}

抱歉,它有点长.所以我的想法是创建一个排序方法,并使用compareTo方法来确定Rational是否在正确的位置,如果不交换的话.但是然后我不确定您是否能够像在数组中一样在列表中移动元素.所以我以为也许我需要实现Collections.sort()方法并覆盖sort方法,但是我遇到了同样的问题.也许我可以使用.toArray吗?

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.

推荐答案

您所做的工作大致正确.

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.

在后台,Collections.sort方法可以将列表的元素复制到数组中,对数组进行排序,然后从排序后的数组中重建列表.实际行为取决于列表实现类.

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.

这篇关于您可以实现并重写list.sort()方法以对有理数列表进行排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆