当有奇数队号时,我的夹具算法会抛出异常
My fixture algorithm throws exception when there are odd team numbers
当团队列表计数为奇数时,我的夹具生成器算法会抛出 java.lang.IndexOutOfBoundsException。我正在发送 21 个大小的团队列表并给出了这个例外。即使是 17、19、23 也会抛出相同的异常。这是异常:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xelorium.soccerleaguetable, PID: 1189
java.lang.IndexOutOfBoundsException: Index: 21, Size: 21
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.get(LinkedList.java:476)
at com.xelorium.soccerleaguetable.FixtureGenerator.getFixtures(FixtureGenerator.java:34)
FixtureGenerator.java:
public class FixtureGenerator<T extends Object> {
public List<List<MatchModel<T>>> getFixtures(List<T> teams, boolean includeReverseFixtures) {
int numberOfTeams = teams.size();
boolean ghost = false;
if (numberOfTeams % 2 != 0) {
numberOfTeams++;
ghost = true;
}
int totalRounds = numberOfTeams - 1;
int matchesPerRound = numberOfTeams / 2;
List<List<MatchModel<T>>> rounds = new LinkedList<List<MatchModel<T>>>();
for (int round = 0; round < totalRounds; round++) {
List<MatchModel<T>> fixtures = new LinkedList<MatchModel<T>>();
for (int match = 0; match < matchesPerRound; match++) {
int home = (round + match) % (numberOfTeams - 1);
int away = (numberOfTeams - 1 - match + round) % (numberOfTeams - 1);
if (match == 0) {
away = numberOfTeams - 1;
}
//Here where it throws the exception
fixtures.add(new MatchModel<T>(teams.get(home), teams.get(away)));
}
rounds.add(fixtures);
}
在这里,您要增加团队数量并尝试匹配一个甚至不存在的团队。
if (numberOfTeams % 2 != 0) {
numberOfTeams++;
ghost = true;
}
假设您有 21 个团队,团队列表中的最后一个索引是 20(索引从零开始)。但是“numberOfTeams”是 22。在这个 if 块中,“away”将是 22 - 1 = 21,然后尝试到达列表中的 21st 索引团队。但是这个团队不存在。
if (match == 0) {
away = numberOfTeams - 1;
}
//Here where it throws the exception
fixtures.add(new MatchModel<T>(teams.get(home), teams.get(away)));
作为一种解决方案,当团队数量为奇数时,您可以将空团队添加到绕过列表。
当团队列表计数为奇数时,我的夹具生成器算法会抛出 java.lang.IndexOutOfBoundsException。我正在发送 21 个大小的团队列表并给出了这个例外。即使是 17、19、23 也会抛出相同的异常。这是异常:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xelorium.soccerleaguetable, PID: 1189
java.lang.IndexOutOfBoundsException: Index: 21, Size: 21
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.get(LinkedList.java:476)
at com.xelorium.soccerleaguetable.FixtureGenerator.getFixtures(FixtureGenerator.java:34)
FixtureGenerator.java:
public class FixtureGenerator<T extends Object> {
public List<List<MatchModel<T>>> getFixtures(List<T> teams, boolean includeReverseFixtures) {
int numberOfTeams = teams.size();
boolean ghost = false;
if (numberOfTeams % 2 != 0) {
numberOfTeams++;
ghost = true;
}
int totalRounds = numberOfTeams - 1;
int matchesPerRound = numberOfTeams / 2;
List<List<MatchModel<T>>> rounds = new LinkedList<List<MatchModel<T>>>();
for (int round = 0; round < totalRounds; round++) {
List<MatchModel<T>> fixtures = new LinkedList<MatchModel<T>>();
for (int match = 0; match < matchesPerRound; match++) {
int home = (round + match) % (numberOfTeams - 1);
int away = (numberOfTeams - 1 - match + round) % (numberOfTeams - 1);
if (match == 0) {
away = numberOfTeams - 1;
}
//Here where it throws the exception
fixtures.add(new MatchModel<T>(teams.get(home), teams.get(away)));
}
rounds.add(fixtures);
}
在这里,您要增加团队数量并尝试匹配一个甚至不存在的团队。
if (numberOfTeams % 2 != 0) {
numberOfTeams++;
ghost = true;
}
假设您有 21 个团队,团队列表中的最后一个索引是 20(索引从零开始)。但是“numberOfTeams”是 22。在这个 if 块中,“away”将是 22 - 1 = 21,然后尝试到达列表中的 21st 索引团队。但是这个团队不存在。
if (match == 0) {
away = numberOfTeams - 1;
}
//Here where it throws the exception
fixtures.add(new MatchModel<T>(teams.get(home), teams.get(away)));
作为一种解决方案,当团队数量为奇数时,您可以将空团队添加到绕过列表。