gpt4 book ai didi

java - 对来自同一类的不同实例的值进行排序

转载 作者:行者123 更新时间:2023-12-02 11:55:05 26 4
gpt4 key购买 nike

我想根据足球俱乐部的积分对它们进行排序。我创建了一个类,它从互联网获取值并处理该值,以根据获胜、失败和平局的数量计算分数。然后我想按顺序将排名及其数据写入文本文件。我为 20 个不同的俱乐部创建了 20 个不同的实例。但是,我不知道排序顺序的最佳方式是什么。

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CalculateStandings {
private int matches;
private int homeWin;
private int homeDraw;
private int homeLost;
private int homeForGoal;
private int homeAgainstGoal;
private int awayWin;
private int awayDraw;
private int awayLost;
private int awayForGoal;
private int awayAgainstGoal;
private int point;
private int totalWin;
private int totalLost;
private int totalDraw;
private int totalAgainstGoal;
private int totalForGoal;


void getResult(String team_Name) {
try {
URL url = new URL("https://raw.githubusercontent.com/openfootball/eng-england/master/2017-18/1-premierleague-i.txt");
Scanner input = new Scanner(url.openStream());
while (input.hasNext()) {
String mydata = input.nextLine();
if (mydata.contains(team_Name)) {
Pattern pattern = Pattern.compile("[0-9]{1,2}-[0-9]{1,2}");
Matcher matcher = pattern.matcher(mydata);
String result;
if (matcher.find()) {
matches += 1;
result = matcher.group();

if (mydata.startsWith(" " + team_Name)) {
homeForGoal += Integer.parseInt(Character.toString(result.charAt(0)));
homeAgainstGoal += Integer.parseInt(Character.toString(result.charAt(2)));
} else if (mydata.endsWith(" " + team_Name)) {
awayForGoal += Integer.parseInt(Character.toString(result.charAt(2)));
awayAgainstGoal += Integer.parseInt(Character.toString(result.charAt(0)));
}

if (Integer.parseInt(Character.toString(result.charAt(0))) > Integer.parseInt(Character.toString(result.charAt(2)))) {
if (mydata.startsWith(" " + team_Name)) {
point += 3;
homeWin += 1;
} else if (mydata.endsWith(" " + team_Name)) {
awayLost += 1;
}
} else if (Integer.parseInt(Character.toString(result.charAt(0))) < Integer.parseInt(Character.toString(result.charAt(2)))) {
if (mydata.startsWith(" " + team_Name)) {
homeLost += 1;
} else if (mydata.endsWith(" " + team_Name)) {
point += 3;
awayWin += 1;
}

} else {
if (mydata.startsWith(" " + team_Name)) {
point += 1;
homeDraw += 1;
} else if (mydata.endsWith(" " + team_Name)) {
point += 1;
awayDraw += 1;
}
}
}
}
}
totalWin = homeWin + awayWin;
totalLost = homeLost + awayLost;
totalDraw = homeDraw + awayDraw;
totalAgainstGoal = homeAgainstGoal + awayAgainstGoal;
totalForGoal = homeForGoal + awayForGoal;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

这是我的主课:

public class Launcher {
public static void main(String[] args) {

CalculateStandings Arsenal = new CalculateStandings();
CalculateStandings Tottenham = new CalculateStandings();
CalculateStandings WHam = new CalculateStandings();
CalculateStandings CPalace = new CalculateStandings();
CalculateStandings MU = new CalculateStandings();
CalculateStandings MC = new CalculateStandings();
CalculateStandings Everton = new CalculateStandings();
CalculateStandings Liv = new CalculateStandings();
CalculateStandings WBAlbion = new CalculateStandings();
CalculateStandings NU = new CalculateStandings();
CalculateStandings Stoke_City = new CalculateStandings();
CalculateStandings Southampton = new CalculateStandings();
CalculateStandings Leicester_City = new CalculateStandings();
CalculateStandings Bournemouth = new CalculateStandings();
CalculateStandings Watford = new CalculateStandings();
CalculateStandings Brighton = new CalculateStandings();
CalculateStandings Burnley = new CalculateStandings();
CalculateStandings Huddersfield = new CalculateStandings();
CalculateStandings Swansea = new CalculateStandings();


Arsenal.getResult("Arsenal FC");
Tottenham.getResult("Tottenham Hotspur");
WHam.getResult("West Ham United");
CPalace.getResult("Crystal Palace");
MU.getResult("Manchester United");
MC.getResult("Manchester City");
Everton.getResult("Everton FC");
Liv.getResult("Liverpool FC");
WBAlbion.getResult("West Bromwich Albion");
NU.getResult("Newcastle United");
Stoke_City.getResult("Stoke City");
Southampton.getResult("Southampton FC");
Leicester_City.getResult("Leicester City");
Bournemouth.getResult("AFC Bournemouth");
Watford.getResult("Watford FC");
Brighton.getResult("Brighton & Hove Albion");
Burnley.getResult("Burnley FC");
Huddersfield.getResult("Huddersfield Town");
Swansea.getResult("Swansea City");

}

}

我正在考虑使用 Getter 将值存储到多个数组中,然后使用冒泡排序等排序算法对它们进行排序。但是,正如您所看到的,实例的数量以及每个实例中的字段数量都非常多,使用 Getter 手动将它们写入数组会花费太多时间。所以想请问一下有没有更优化的方法。

最佳答案

使用列表而不是数组。您还应该检查 Comparable 接口(interface)以及如何使用它。这样稍后您就可以在列表上使用排序方法。

https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

关于java - 对来自同一类的不同实例的值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658346/

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