ArrayList排序锦标赛排名

时间:2014-12-16 03:21:47

标签: java sorting arraylist collections

好吧,我不能为我的生活弄清楚如何通过锦标赛排名来排序我的数据。这是我的代码。

if (o == 5) {
    double RD, t, old, x;
    String tournament, player;
    int a, number_of_players, place;
    place = 0;
    ArrayList<player> players = new ArrayList<player> ();
    ArrayList<placeDisplay> placeVar = new ArrayList<placeDisplay> ();
    List<placeDisplay> sort = new ArrayList<placeDisplay> ();

    System.out.println("1:Add a tournament \t2:View Existing");
    a = keyIn.nextInt();

    if (a == 1) {
        System.out.println("\nEnter tournament name");
        tournament = keyIn.next();
        System.out.println("\nEnter number of players");
        number_of_players = keyIn.nextInt();
        System.out.println("Enter players");

        for (int i = 0; i < number_of_players; i++) {
            String name = keyIn.next();
            player plr = new player();
            plr.setName(name);
            players.add(plr);
        }

        System.out.println("\nEnter places for");
        System.out.println(players);

        for (int i = 0; i < players.size(); i++) {
            System.out.println("\n" + players.get(i));
            int places = keyIn.nextInt();
            placeDisplay placer = new placeDisplay();
            placer.setPlace(places);
            placeVar.add(placer);
        }

        Collections.sort(sort);
        System.out.println("\nThe Places are as follows");

        for (int i = 0; i < players.size() && i < placeVar.size(); i++) {
            System.out.println(placeVar.get(i) + ":" + players.get(i));
        }
    }
}

这是我的公共placeDisplay类文件。

public class placeDisplay implements Comparable<placeDisplay> {

    private int places;

    public void setPlace(int nPlace) {
        places = nPlace;
    }

    public int getPlace() {
        return places;
    }

    @Override
    public String toString() {
        return Integer.toString(places);
    }

    @Override
    public int compareTo(placeDisplay placeDisplay){
        if (places > places)
            return 1;
        else if (places == places)
            return 0;
        else 
            return -1;
    }
}

这是公共类文件

 public class player {

private String name;

public void setName(String pName)
{
    name = pName;
}

public String getName()
{

    return name;
}

@Override
public String toString() {
    return name;
}

}

这是我对该程序部分的结果。希望你们能帮我解决这个问题!

1:Add a tournament  2:View Existing
1

Enter tournament name
tournament1

Enter number of players
3
Enter players
Bob
Sally
John

Enter places for
[Bob, Sally, John]

Bob
2

Sally
1

John
3

The Places are as follows
2:Bob
1:Sally
3:John

Return to Main Menu? (Y/N)

2 个答案:

答案 0 :(得分:2)

需要修改比较方法

public int compareTo(placeDisplay placeDisplay){
        if (places > placeDisplay.places)
            return 1;
        else if (places == placeDisplay.places)
            return 0;
        else 
            return -1;
    }

答案 1 :(得分:0)

您的compareTo不正确,因为您正在比较同一个变量。 将其更改为:

@Override
public int compareTo(placeDisplay placeDisplay){
    if (places > placeDisplay.getPlace())
        return 1;
    else if (places == placeDisplay.getPlace())
        return 0;
    else 
        return -1;
}

甚至简单地说:

@Override
public int compareTo(placeDisplay placeDisplay){
    return places - placeDisplay.getPlace();
}

我还建议您覆盖equals方法,使其与您的compareTo一致,如Comparable接口文档中所建议的那样:&#34; C类的自然顺序当且仅当e1.compareTo(e2)== 0与c1的每个e1和e2的e1.equals(e2)具有相同的布尔值时才被认为与equals一致。注意null不是任何实例class,和e.compareTo(null)应抛出NullPointerException,即使e.equals(null)返回false。&#34;