2014-02-19 44 views
0

涉及到相當多的代碼,我不確定需要多少細節,但是我使用mysql表中的數據填充了一個樹形圖,並且我正在麻煩遍歷它。對從MySQL表填充的數組進行迭代

下面是類包含的映像樹

public class SeasonResults{ 

private static Map<String,SeasonResults> allResults = new TreeMap<String,SeasonResults>(); 
private String hometeam; 
private String awayteam; 
private String result;           

private static SeasonResults result6; 

public static SeasonResults add(String hometeam, String awayteam, String result) 
{ 
     result6 = new SeasonResults(hometeam, awayteam, result); 
     allResults.put(hometeam,result6); 

    return result6; 
} 


private SeasonResults(String hometeam, String awayteam, String result) 
{ 
    this.hometeam = hometeam; 
    this.awayteam = awayteam; 
    this.result = result; 
} 

public static Collection<SeasonResults> getCollection() 
{ 
    return allResults.values(); 
} 

@Override 
public String toString() 
{ 
    return " "+hometeam+", "+awayteam+", "+result; 
} 

}

這裏的代碼就是我填充數組,然後嘗試,並遍歷它的代碼。

public void HeadToHead(){ 
    try 
    { 
     //Sets up the connedtion to the database and installs drivers which are required. 
     Class.forName("com.mysql.jdbc.Driver");                   
     con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");   

     String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?"; 
     PreparedStatement prepst;    

     prepst = con.prepareStatement(SQL); 
     prepst.setString(1,box1.getSelectedItem().toString()); 
     prepst.setString(2,box2.getSelectedItem().toString()); 
     rs = prepst.executeQuery(); 

     while (rs.next()) 
     { 
      //This retrieves each row of League table and adds it to an array in the League Results class. 

      hometeam = rs.getString("HomeTeam"); 
      awayteam = rs.getString("AwayTeam");      
      result = rs.getString("Result");      

      custs = (hometeam + "," + awayteam + "," + result);  // Takes all the variables containging a single customers information and puts it into a string, seperated by commas. 
      SeasonResults.add(hometeam, awayteam, result); 
     } 
    } 
    catch (Exception e) 
    { 
     System.out.println("Error " +e);                    
    } 

    Seasonrecord = SeasonResults.getCollection(); 
    seasons = new SeasonResults[Seasonrecord.size()]; 
    Iterator iterateSeason = Seasonrecord.iterator(); 
    int i = 0; 

    while(iterateSeason.hasNext()){ 
     seasons[i] = (SeasonResults)iterateSeason.next(); 
     i++; 

     if(result.equals("HW")){ 
      hometeamvalue = hometeamvalue + 50; 
     } 
     else if(result.equals("D")){ 
      hometeamvalue = hometeamvalue + 10; 
      awayteamvalue = awayteamvalue + 10; 
     } 
     else{ 
      if(result.equals("AW")){ 
       awayteamvalue = awayteamvalue + 50; 
      } 
     } 
    } 
} 

有數據庫5 '結果' 場。 2是'HW',2是'AW',1是'D'。我想要做的是打印出'hometeamvalue'和'outteamvalue',但是當我這樣做時,打印值僅爲10.只使用第一個字段的值。

當我想在GUI中顯示結果並顯示所有字段時,我使用相同的代碼遍歷數組。但是當我嘗試和他們做一些計算時,這是行不通的。

任何想法是什麼問題?

+0

如果( result.equals( 「HW」 ))在這個語句中替換結果與季節[i] .getResult()。equals(「HW」)和在其餘兩個if語句也做同樣的..並嘗試 – Naren

+0

試過了,但它不起作用。我不認爲這部分是問題,因爲它對數組中的第一項工作正常,但它不會進一步得到其他人。 – user3139748

+0

它會工作,但你需要改變你的Serachesults Code也可以完全發佈SeasonResults.java,併爲什麼你採取私有靜態地圖 allResults在同一類... – Naren

回答

0

你要做這樣的

SeasonResults.java

public class SeasonResults{ 
private String hometeam; 
private String awayteam; 
private String result;           

public String getHometeam() { 
    return hometeam; 
} 
public void setHometeam(String hometeam) { 
    this.hometeam = hometeam; 
} 
public String getAwayteam() { 
    return awayteam; 
} 
public void setAwayteam(String awayteam) { 
    this.awayteam = awayteam; 
} 
public String getResult() { 
    return result; 
} 
public void setResult(String result) { 
    this.result = result; 
} 

public SeasonResults(String hometeam, String awayteam, String result) 
{ 
    this.hometeam = hometeam; 
    this.awayteam = awayteam; 
    this.result = result; 
} 
@Override 
public String toString() 
{ 
    return " "+hometeam+", "+awayteam+", "+result; 
} 

} 

HeadToHaed方法

public void HeadToHead(){ 
     String hometeam,awayteam,result; 
     int hometeamvalue,awayteamvalue; 
      List<SeasonResults> allResults = new ArrayList<SeasonResults>(); 

     try 
     { 
      //Sets up the connedtion to the database and installs drivers which are required. 
      Class.forName("com.mysql.jdbc.Driver");                   
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");   

      String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?"; 
      PreparedStatement prepst;    

      prepst = con.prepareStatement(SQL); 
      prepst.setString(1,box1.getSelectedItem().toString()); 
      prepst.setString(2,box2.getSelectedItem().toString()); 
      ResultSet rs = prepst.executeQuery(); 

      SeasonResults seasonResults=null; 
      while (rs.next()) 
      { 

       //This retrieves each row of League table and adds it to an array in the League Results class. 

       hometeam = rs.getString("HomeTeam"); 
       awayteam = rs.getString("AwayTeam");      
       result = rs.getString("Result");      

       seasonResults=new SeasonResults(hometeam, awayteam, result) ; 
       custs = (hometeam + "," + awayteam + "," + result);  // Takes all the variables containging a single customers information and puts it into a string, seperated by commas. 
       allResults.add(seasonResults); 
      } 
     } 
     catch (Exception e) 
     { 
      System.out.println("Error " +e);                    
     } 

     System.out.println("SIze of ArrayList::"+allResults.size()); 
     for(SeasonResults temp:allResults) 
     { 
       if(temp.getResult().equals("HW")){ 
        hometeamvalue = hometeamvalue + 50; 
       } 
       else if(temp.getResult().equals("D")){ 
        hometeamvalue = hometeamvalue + 10; 
        awayteamvalue = awayteamvalue + 10; 
       } 
       else{ 
        if(temp.getResult().equals("AW")){ 
         awayteamvalue = awayteamvalue + 50; 
        } 
       } 

     } 

    } 

讓我知道如果ü面對任何問題

+0

它的工作原理。非常感謝你,這一直是一個問題,我的最後一個星期。 – user3139748