2017-10-12 11 views
0

私の理解は、静的変数が変数に存在するクラスのすべてのインスタンス間で共有されるように存在することで次のように静的ArrayListの

私のコードは次のとおり。

public class Game { 

    private static final ArrayList<Game> GAMESLIST = new ArrayList<Game>(); 
    Random rand = new Random(); 

    private int gameID; 
    private int teamScore1; 
    private int teamScore2; 
    private int temperature; 
    private String teamName1; 
    private String teamName2; 

    public void PlayGame(Team team1, Team team2, int value, String teamNameValue1, String teamNameValue2, Scheduler scheduler){ 

     temperature = value; 
     int maxGoals = 2; 
     int iterator; 

     if(temperature > 12){ 
      for(iterator = 0; iterator < temperature; ++iterator){ 
       ++maxGoals; 
       if(maxGoals == 8){ 
        break; 
       } 
      } 
     } 

     teamScore1 = rand.nextInt(maxGoals); 
     teamScore2 = rand.nextInt(maxGoals); 
     teamName1 = teamNameValue1; 
     teamName2 = teamNameValue2; 

     ++gameID; 
     System.out.println(teamScore1); 
     System.out.println(teamScore2); 

     GAMESLIST.add(this); 
     scheduler.PlayGame(); 
    } 

    public void PrintStatistics(){ 
     int iterator; 

     for(iterator = 0; iterator < this.GAMESLIST.size(); ++iterator){ 
      System.out.println("Game# " +this.GAMESLIST.get(iterator).gameID); 
      System.out.println("Team 1: " +this.GAMESLIST.get(iterator).teamName1); 
      System.out.println("Team 2: " +this.GAMESLIST.get(iterator).teamName2); 

      System.out.println(GAMESLIST.get(iterator).teamScore1); 
      System.out.println(GAMESLIST.get(iterator).teamScore2); 
      System.out.println("Recorded temperature that day: " + GAMESLIST.get(iterator).temperature); 
     } 
    } 


public class Scheduler { 

    Random rand = new Random(); 

    private Team[] teams; 
    private Team team1, team2; 
    private Game game; 
    private int temperature; 
    private int numberOfColdDays = 0; 

    public Scheduler(){ 

    } 

    public Scheduler(Game gameValue, Team[] teamsValue){ 
     game = gameValue;  
     teams = teamsValue; 
    } 

    public void PlayGame(){ 

     if(IsTooCold() == true){ 
      System.out.println("Too cold to play!"); 
      ++numberOfColdDays; 
      if(numberOfColdDays < 3){ 
       SoccerLeague.PlayGame(this); 
      } 
      else{ 
       SoccerLeague.EndSeason(this); 
      } 
     } 
     else{ 
      numberOfColdDays = 0; 
      TeamPicker(teams); 
      game.PlayGame(team1, team2, temperature, team1.teamName, team2.teamName, this); 
     } 
    } 

    public void TeamPicker(Team[] teams){ 
     int value1; 
     int value2; 

     value1 = rand.nextInt(3); 
     team1 = teams[value1]; 

     do{ 
      value2 = rand.nextInt(3); 
      team2 = teams[value2]; 
     }while(value1 == value2); 
    }  

    public boolean IsTooCold(){ 
     boolean tOrF = false; 

     System.out.println("Is it too cold to play?"); 
     this.temperature = rand.nextInt(30); 

     if(temperature < 7){ 
      tOrF = true; 
     } 
     return tOrF; 
    } 

    public void PrintGames(){ 

    } 
} 

IDEでデバッガを実行すると、Gameの1つのインスタンスのArrayListの内部に無制限のthisオブジェクトが作成されます。また、特定の条件が満たされた後にArrayListを印刷すると、ループは1回の繰り返しでランダムに印刷され、以前に作成された1回の反復のインスタンスがランダムに印刷されます。

Gameクラスのコードが、PlayGameメソッドまたはPrintStatisticsメソッドのどこかで失敗していますか?

また、PrintStatisticsメソッドのループからthisキーワードを完全に削除しようとしましたが、同じ結果が得られます。

いつもどんな助けもありがとうございます。

編集要求通り、スケジューラクラス。

よろしくお願いします。MYLESMAN

+0

ちょっとメモ:静的変数はクラス以外のインスタンスには属しません。静的変数の現在のオブジェクトインスタンスを参照する 'this'を使用することはほとんど意味がありません。代わりに静的フィールドを参照するためにclassnameを使用してください: 'this.GAMESLIST'の代わりに' Game.GAMESLIST' –

+2

'scheduler'とは何ですか?おそらくあなたは再帰を持っているでしょうが、文脈なしでは話すことは不可能です。 –

+0

'PrintStatistics()'も静的でなければなりません。デバッガがあなたに提供する情報に注意を払ってください。 '... @ 123' - これはオブジェクトの一意の' id'です。私は確信しています、あなたの 'this.GAMELIST' - はすべて同じオブジェクトです。 –

答えて

0

あなたは思いがけない無限ループをしています。

あなたのゲームをリストに追加し、scheduler.PlayGame()メソッドを呼び出すGame.PlayGameメソッドがあります。ただし、scheduler.PlayGame()は、game.PlayGame()を順番に呼び出します。それはガメリストに何度もゲームを追加することにつながり、StackOverflowErrorとOutOfMemoryErrorにつながる可能性があります。

関連する問題