gpt4 book ai didi

java - 如何按升序对集合中的元素进行排序?

转载 作者:行者123 更新时间:2023-12-01 11:54:27 26 4
gpt4 key购买 nike

我将足球元素添加到设定的球队中,但我想按照我计算出的分数升序对它们进行排序...我在足球类中已经有一个compareTo方法。我想知道按升序对集合进行排序的最有效方法是什么。我得到了相同的顺序

这是一些测试代码

 Georgia Southern(Sun Belt)|9|0|0|0|0|0|0|1
Louisana-Lafayette(Sun Belt)|36|0|0|0|0|4|0|1
Appalachian State(Sun Belt)|7|0|0|0|0|0|0|0
Texas State(Sun Belt)|17|0|0|0|0|0|0|0
Arakansas State(Sun Belt)|35|0|0|0|0|2|2|3
South Alabama(Sun Belt)|14|0|0|0|0|0|1|0


import java.util.*;
import java.util.Collection;
import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Schedule
{
public static void main (String args[])throws IOException
{
Scanner sc=new Scanner(new File("Footballteams.txt"));
Set<Football>teams=new TreeSet<Football>();

for(int i=0;i<128;i++)
{
String team=sc.nextLine();
Integer points=0;
String[]a=team.split("\\|",9);
String name=a[0];
int wins=Integer.parseInt(a[1]);
points+=wins;
int finalRecord14=Integer.parseInt(a[2]);
int finalRecord13=Integer.parseInt(a[3]);
int finalRecord12=Integer.parseInt(a[4]);
int finalRecord11=Integer.parseInt(a[5]);
int bowlVictories=Integer.parseInt(a[6]);
points=points+(bowlVictories*10);
int bowlLosses=Integer.parseInt(a[7]);
points=points-(bowlLosses*5);
int ConferenceChamp=Integer.parseInt(a[8]);
points=points+(ConferenceChamp*10);
Football x=new Football(name,wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamp,points);
teams.add(x);


}

for(Football x:teams)
System.out.println(x.name);











}

}

 public class Football implements Comparable<Football>
{
public String name;
public int wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamps;
public Integer points;
public Football(String name,int wins,int finalRecord14,int finalRecord13,int finalRecord12,int finalRecord11,int bowlVictories,int bowlLosses,int ConferenceChamp,Integer points)
{
this.name=name;
this.wins=wins;
this.finalRecord14=finalRecord14;
this.finalRecord13=finalRecord13;
this.finalRecord12=finalRecord12;
this.finalRecord11=finalRecord11;
this.bowlVictories=bowlVictories;
this.bowlLosses=bowlLosses;
this.ConferenceChamps=ConferenceChamp;
this.points=points;
}
public String getName()
{
return name;
}
public int getWins()
{
return wins;
}
public int getfinalRecord14()
{
return finalRecord14;
}
public int getfinalRecord13()
{
return finalRecord13;
}
public int getfinalRecord12()
{
return finalRecord12;
}
public int getfinalRecord11()
{
return finalRecord11;
}
public int getBowlVictories()
{
return bowlVictories;
}
public int getBowlLosses()
{
return bowlLosses;
}
public int getConferenceChamps()
{
return ConferenceChamps;
}
public Integer getPoints()
{
return points;
}
public int compareTo(Football o)
{

return getPoints().compareTo(o.getPoints());
}

}

最佳答案

您的 Football 类应实现 Comparable 接口(interface)并重写其 compareTo 方法。

public class Football implements Comparable<Football> {
protected String name;
protected Integer points;

public Football(String name, Integer points) {
setName(name);
setPoints(points);
}

// getters & setters

@Override
public int compareTo(Football o) {
return getPoints().compareTo(o.getPoints());
}
}

安排模拟:

import java.util.Set;
import java.util.TreeSet;

public class Schedule {
public static void main(String[] args) {
Set<Football> teams = new TreeSet<>();

teams.add(new Football("Team A", 3));
teams.add(new Football("Team B", 1));
teams.add(new Football("Team C", 2));

for (Football x : teams) {
System.out.println(x.getName());
}
}
}

根据各队得分输出:

Team B
Team C
Team A

关于java - 如何按升序对集合中的元素进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com