0

I'm new to Java and I'm currently writing a program for an assignment that represents a 'sports league' (classes to represent player/club/match/league)

My main problems are occurring in the league class. Here are the relevant variables to give you an idea how I'm storing things:

public class League 
{
    private String leagueName;
    private ArrayList<Club> clubs;
    private ArrayList<Match> fixtures;
    private ArrayList<String> results2;
    private TreeMap<Match, String> results;
    private String topTeam;
    private String goldenBoot;

}

Currently trying to write a method in the League class which will print a 'league table' - i.e. a list of Clubs sorted by their points tally (held as variable in Club class) and I'm drawing a blank on it.

Further to this, I need to write two methods to find the top scorer (golden boot) and find the top team in the league; again I am drawing a blank. Perhaps I am overcomplicating things?

Would be very grateful for suggestions/sample methods

EDIT:

Ok, so that method I'm trying to write is something beginning with:

public void getLeagueTable() {
    for(Club c : clubs) {
        c.getTally();
    }
}

which would give the tally value for each Club object - but how to sort these results, and how to associate the highest with one Club is what's really troubling.

4

2 回答 2

0

要打印联赛表,您需要对俱乐部数组进行排序,然后遍历每个项目并打印俱乐部名称。

要对俱乐部数组进行排序,请尝试使用 Collections.sort http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html

..这是假设您没有被告知要实现自己的算法

同样,要获得最佳球队和最佳射手,需要进行更多排序,然后您需要从排序列表中选择最上面的项目。

希望有帮助...

于 2012-12-05T17:43:07.107 回答
0

你最好使用Set而不是ArrayList. 这是您问题的一个良好开端:

void printLeagueTable(){

    i = 0 ;
    while( i != clubs.size() ){
         Club club = clubs.get(i);
         System.out.println("club: "+i+ "points: " club.points() );  
    }

}
于 2012-12-05T17:45:25.463 回答