我想根据他们的观点对足球俱乐部的顺序进行排序。我创建了一个类,它从互联网获取值并处理该值以根据获胜,失败和绘制的数量计算点数。然后我想按顺序将标题及其数据写入文本文件。我为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将值存储到多个数组中,然后使用排序算法(如Bubble sort)对它们进行排序。但是,正如您所看到的,每个实例中的实例数和字段数很多,使用Getter手动将它们写入数组需要花费太多时间。因此,我想问你是否有更优化的方法。
答案 0 :(得分:1)
使用List而不是数组。您还应该检查Comparable接口以及如何使用它。以后您可以在List上使用sort方法。
https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
答案 1 :(得分:0)
您可以使用:
ArrayList < CalculateStandings > footballClubs = new ArrayList < CalculateStandings >();
在主类中创建对象数组,然后使用
void add(footballClubName: CalculateStandings)
将足球俱乐部实例添加到arrayList的方法。
- 强制对对象进行排序的下一步是通过编辑类的头来将ComplateStandings类实现到Comparable接口
class CalculateStandings implements Comparable < CalculateStandings >
并覆盖类体中compareTo方法的实现,以便如何比较此类的实例。就像这个
@Override
public int compareTo(CalculateStandings o) {
if(somthing_field(as_your_calculations) > o.somthing_field)
return 1;
else if (the_something_field < o.something_field)
return -1;
else
return 0;
}
- 然后在主类中轻松使用java.util.Collections。 sort(arraylist)以递增顺序对其进行排序,如此
java.util.Collections.sort(footballClubs);
然后如果你想按顺序递减它,你可以使用for循环来反转它。
这就是全部,但我对你的代码有一个侧面说明,你可以缩短它并通过使用带有args的教师而不是这个无法教练来集中阅读,并使用俱乐部名称字符串作为你的args和在教师的实现中使用你的计算而不需要使用实例方法,
public CalculateStandings(String footballClubName) {
//put your calculations here and assign the class fields.
}
只需使用教师实例化一个并将它们添加到数组中,就像这样
footballClubs.add(new CalculateStandings(footballClubName));
我希望它有所帮助。
答案 2 :(得分:0)
你可以有一个单独的地图,其中俱乐部名称作为键,点数作为值,你可以使用lambdas打印出表格,这里是一个根据点(降序)对俱乐部进行排序的例子:
package com.company;
import java.util.HashMap;
import java.util.Map;
public class Main {
private static int rank = 1;
public static void main(String[] args) {
Map<String, Integer> teams = new HashMap<>(){{
put("Arsenal", 54);
put("Chelsea", 44);
put("Manchester United", 52);
put("Liverpool", 49);
put("Tottenham", 50);
}};
teams.entrySet().stream()
.sorted((x,y) -> y.getValue().compareTo(x.getValue()))
.forEach(n -> System.out.println(rank++ + ". " + n.getKey() + " with " + n.getValue() + " points."
));
}
}
输出结果为:
阿森纳得到54分。
曼联得到52分。
托特纳姆得到50分。
利物浦得到49分。
切尔西得到44分。
如果您有任何其他问题,请在评论中问我。