Java – 避免/解决堆栈溢出

huangapple go评论124阅读模式
英文:

Java - Avoid/Solve StackOverFlow

问题

这个问题出现在你的循环中。循环中的pistes数组的长度是5,但matches的大小却可能小于5,因此你需要确保pistes数组不越界。你可以使用取模运算来处理这个问题,如下所示:

for (Round round : rounds) {
    System.out.println("Round " + round.getNumber());
    List<Match> matches = round.getMatches();
    for (int i = 0; i < pistes.length; i++) {
        if (i < matches.size()) {
            Match match = matches.get(i);
            String piste = pistes[i];
            System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
        }
    }
    System.out.println();
}

这将确保只在matches中有匹配时才使用pistes中的元素,防止数组越界错误。

英文:

this is the code

import java.util.ArrayList;
import java.util.List;

public class ScheduleGenerator {
    public static void main(String[] args) {
        List&lt;Team&gt; teams = new ArrayList&lt;&gt;();
        int numTeams = 9;
        for (int i = 1; i &lt;= numTeams; i++) {
            teams.add(new Team(&quot;Team &quot; + i));
        }
        String[] pistes = {&quot;Piste A&quot;, &quot;Piste B&quot;, &quot;Piste C&quot;, &quot;Piste D&quot;, &quot;Piste E&quot;, &quot;Piste F&quot;};
        List&lt;Round&gt; rounds = generateSchedule(teams);

        for (Round round : rounds) {
            System.out.println(&quot;Round &quot; + round.getNumber());
            List&lt;Match&gt; matches = round.getMatches();
            for (int i = 0; i &lt; matches.size(); i++) {
                Match match = matches.get(i);
                String piste = pistes[i];
                System.out.println(piste + &quot; &quot; + match.getTeam1().getName() + &quot; vs &quot; + match.getTeam2().getName());
            }
            System.out.println();
        }
    }

    public static List&lt;Round&gt; generateSchedule(List&lt;Team&gt; teams) {
        int numTeams = Math.round(teams.size());
        int numRounds = numTeams;
        int halfSize = numTeams / 2;

        List&lt;Team&gt; list = new ArrayList&lt;&gt;(teams);

        int listSize = list.size();

        List&lt;Round&gt; rounds = new ArrayList&lt;&gt;();

        if (numTeams % 2 != 0) {
            teams.add(new Team(&quot;Rest&quot;));
            numTeams++;
        }


        for (int round = 0; round &lt; numRounds; round++) {
            Round currentRound = new Round(round);
            List&lt;Match&gt; matches = new ArrayList&lt;&gt;();
            int teamIdx = round % listSize;
            Team team1 = list.get(teamIdx);
            Team team2 = teams.get(numTeams - 1);
            matches.add(new Match(team1, team2));
            for (int i = 1; i &lt; halfSize; i++) {
                int firstTeam = (round + i) % listSize;
                int secondTeam = (round + listSize - i) % listSize;
                matches.add(new Match(list.get(firstTeam), list.get(secondTeam)));
            }
            currentRound.setMatches(matches);
            rounds.add(currentRound);
        }

        return rounds;
    }
}

class Team {
    private String name;

    public Team(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return name;
    }
}

class Round {
    private int number;
    private List&lt;Match&gt; matches;

    public Round(int number) {
        this.number = number;
        this.matches = new ArrayList&lt;&gt;();
    }

    public int getNumber() {
        return number;
    }

    public void addMatch(Match match) {
        matches.add(match);
    }
    public void setMatches(List&lt;Match&gt; matches) {
        this.matches = matches;
    }

    public List&lt;Match&gt; getMatches() {
        return matches;
    }

    public boolean hasMatch(Team team1, Team team2) {
        for (Match match : matches) {
            if ((match.getTeam1() == team1 &amp;&amp; match.getTeam2() == team2) ||
                    (match.getTeam1() == team2 &amp;&amp; match.getTeam2() == team1)) {
                return true;
            }
        }
        return false;
    }
}

class Match {
    private Team team1;
    private Team team2;

    public Match(Team team1, Team team2) {
        this.team1 = team1;
        this.team2 = team2;
    }

    public Team getTeam1() {
        return team1;
    }

    public Team getTeam2() {
        return team2;
    }
}

The output needs to be like this

Round 0
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5

Round 1
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5

Round 2
Piste A Team 9 vs Team 8
Piste B Team 1 vs Team 7
Piste C Team 2 vs Team 6
Piste D Team 3 vs Team 5
Piste E Rest vs Team 4

Round 3
Piste A Team 8 vs Team 7
Piste B Team 9 vs Team 6
Piste C Team 1 vs Team 5
Piste D Team 2 vs Team 4
Piste E Rest vs Team 3

Round 4
Piste A Team 7 vs Team 6
Piste B Team 8 vs Team 5
Piste C Team 9 vs Team 4
Piste D Team 1 vs Team 3
Piste E Rest vs Team 2

Round 5
Piste A Team 6 vs Team 5
Piste B Team 7 vs Team 4
Piste C Team 8 vs Team 3
Piste D Team 9 vs Team 2
Piste E Rest vs Team 1

Round 6
Piste A Team 5 vs Team 4
Piste B Team 6 vs Team 3
Piste C Team 7 vs Team 2
Piste D Team 8 vs Team 1
Piste E Rest vs Team 9

Round 7
Piste A Team 4 vs Team 3
Piste B Team 5 vs Team 2
Piste C Team 6 vs Team 1
Piste D Team 7 vs Team 9
Piste E Rest vs Team 8

Round 8
Piste A Team 3 vs Team 2
Piste B Team 4 vs Team 1
Piste C Team 5 vs Team 9
Piste D Team 6 vs Team 8
Piste E Rest vs Team 7

Round 9
Piste A Team 2 vs Team 1
Piste B Team 3 vs Team 9
Piste C Team 4 vs Team 8
Piste D Team 5 vs Team 7
Piste E Rest vs Team 6

but as you can see in the first bit of the code here

for (Round round : rounds) {
            System.out.println(&quot;Round &quot; + round.getNumber());
            List&lt;Match&gt; matches = round.getMatches();
            for (int i = 0; i &lt; matches.size(); i++) {
                Match match = matches.get(i);
                String piste = pistes[i];
                System.out.println(piste + &quot; &quot; + match.getTeam1().getName() + &quot; vs &quot; + match.getTeam2().getName());
            }
            System.out.println();
        }

this loop makes it that the maximum index is 4 that means that every time I run this code it will be missing Piste E and the output will be something like this

Round 0
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7

Round 1
Piste A Team 2 vs Rest
Piste B Team 3 vs Team 1
Piste C Team 4 vs Team 9
Piste D Team 5 vs Team 8

Round 2
Piste A Team 3 vs Rest
Piste B Team 4 vs Team 2
Piste C Team 5 vs Team 1
Piste D Team 6 vs Team 9

Round 3
Piste A Team 4 vs Rest
Piste B Team 5 vs Team 3
Piste C Team 6 vs Team 2
Piste D Team 7 vs Team 1

Round 4
Piste A Team 5 vs Rest
Piste B Team 6 vs Team 4
Piste C Team 7 vs Team 3
Piste D Team 8 vs Team 2

Round 5
Piste A Team 6 vs Rest
Piste B Team 7 vs Team 5
Piste C Team 8 vs Team 4
Piste D Team 9 vs Team 3

Round 6
Piste A Team 7 vs Rest
Piste B Team 8 vs Team 6
Piste C Team 9 vs Team 5
Piste D Team 1 vs Team 4

Round 7
Piste A Team 8 vs Rest
Piste B Team 9 vs Team 7
Piste C Team 1 vs Team 6
Piste D Team 2 vs Team 5

Round 8
Piste A Team 9 vs Rest
Piste B Team 1 vs Team 8
Piste C Team 2 vs Team 7
Piste D Team 3 vs Team 6

Round 9
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7

I am just really ignorant with this type of error it's been almost 3 days with me trying to figure out a thing. Thanks in advance

答案1

得分: 0

int halfSize = numTeams / 2; 应该改成 int halfSize = numTeams / 2 + 1;

英文:

Just Figure it out int halfSize = numTeams / 2; should be added by one like this int halfSize = numTeams / 2 +1;

huangapple
  • 本文由 发表于 2023年4月11日 02:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定