2017-10-12 58 views
0

我理解的是一個靜態變量將存在於跨該變量駐留在類的所有實例共享靜態數組列表創建的本身無限實例

我的代碼如下:

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類型的一個實例,我在ArrayList內獲得了無限量的this對象實例。

此外,在滿足特定條件並打印ArrayList時,for循環將隨機打印一次迭代並打印隨後創建的一個迭代的實例。

我的代碼在Game類中的某處失敗,在PlayGame方法或PrintStatistics方法中失敗了嗎?

此外,請注意,我已嘗試從PrintStatistics方法的循環中完全刪除this關鍵字,但仍得到相同的結果。

一如既往,任何幫助表示讚賞。

編輯根據要求,調度程序類。

問候,邁爾斯曼

+0

請注意:靜態變量不屬於任何實例,但屬於類。使用引用當前對象實例的this這個靜態變量是沒有意義的。改爲使用classname來引用靜態字段:'Game.GAMESLIST'而不是'this.GAMESLIST' –

+2

什麼是'scheduler'?很可能你有遞歸,但沒有上下文是不可能分辨的。 –

+0

'PrintStatistics()'也應該是靜態的。注意調試器提供給你的信息'... @ 123' - 這是對象唯一的'id'。我相信,你所有的'this.GAMELIST' - 是同一個對象。 –

回答

0

你有不明顯的無盡循環。

您有一個方法Game.PlayGame,它將您的遊戲添加到列表並調用scheduler.PlayGame()。但是scheduler.PlayGame()輪到它調用game.PlayGame()。它導致多次將遊戲添加到遊戲列表,並可能導致StackOverflowError和OutOfMemoryError。