我有一个足球联赛的记分牌。 我想对每支球队进行排名。 为此,我想将每场比赛与每个队进行比较,并将结果放入矩阵中。
哪个系列最好?我试过ArrayList,但我很失望!
ScoreBoard的一个例子:
TEAM1| SCORE | TEAM2 | SCORE
AAAA | 20 | BBBB | 10
AAAA | 12 | CCCC | 14
BBBB | 1 | AAAA | 2
我想像这样建立一个矩阵:任何帮助?任何提示?任何提示都会有所帮助!
AAAA BBBB CCCC
AAAA 0 abs(2-1) 0
BBBB abs(20-10) 0 0
CCCC abs(12-14) 0 0
我忘了说我有一个Class ScoreBoard 变量
String name1,name2;
double score1,score2;
答案 0 :(得分:1)
为此使用类和比较器。
public class ScoreBoard implements Comparator<ScoreBoard>{
String team1, team2;
double team1_score, team2_score;
@Override
public int compare(ScoreBoard s1, ScoreBoard s2) {
//Sort anyway you wish in here
return Math.abs(s1.team1_score - s1.team2_score) - Math.abs(s2.team1_score - s2.team2_score);
}
public void createAndSort() {
List<ScoreBoard> boards = new LinkedList<ScoreBoard>();
//...
//boards.add(....);
Collections.sort(boards, this);
}
}
用法,填充ScoreBoards列表并将其存储在List中。至于你的矩阵,我不确定我理解你的意思
我想像这样建立一个矩阵......
答案 1 :(得分:0)
我为您的问题运行和评论创建了一个程序(请提供建议)
我的节目
import java.util.*;
class Matches
{
static LinkedList MatchesList = new LinkedList();
public void addMatches(ScoreBoard SBtemp)
{
MatchesList.add(SBtemp);
}
public LinkedList getMatches()
{
return MatchesList;
}
}
class ScoreBoard
{
ScoreBoard(String name1,String name2,Double score1,Double score2)
{
Matches addmat=new Matches();
this.name1=name1;
this.name2=name2;
this.score1=score1;
this.score2=score2;
this.diff=Math.abs(this.score1-this.score2);
addmat.addMatches(this);
}
String name1,name2;
Double score1,score2,diff;
}
public class Mycode{
public static void main(String []args){
ScoreBoard SB1=new ScoreBoard("AAAA","BBBB",20.0,10.0);
ScoreBoard SB2=new ScoreBoard("AAAA","CCCC",12.0,14.0);
ScoreBoard SB3=new ScoreBoard("BBBB","AAAA",1.0,2.0);
//default value for matrix
Double Matrix[][]=new Double[3][3];
for(int n=0;n<3;n++)
for(int m=0;m<3;m++)
Matrix[m][n]=0.0;
Matches dismat=new Matches();
LinkedList MatchesList = new LinkedList();
MatchesList=dismat.getMatches();
//create matrix
int m=0,n=0;
for(Iterator i = MatchesList.iterator();i.hasNext();)
{
ScoreBoard SBtemp2 = (ScoreBoard)i.next();
if(SBtemp2.name1.equals("AAAA"))
{
m=0;
if(SBtemp2.name2.equals("BBBB"))
{
n=1;
Matrix[m][n]=SBtemp2.diff;
}
else if(SBtemp2.name2.equals("CCCC"))
{
n=2;
Matrix[m][n]=SBtemp2.diff;
}
}
if(SBtemp2.name1.equals("BBBB"))
{
m=1;
if(SBtemp2.name2.equals("AAAA"))
{
n=0;
Matrix[m][n]=SBtemp2.diff;
}
else if(SBtemp2.name2.equals("CCCC"))
{
n=2;
Matrix[m][n]=SBtemp2.diff;
}
}
if(SBtemp2.name1.equals("CCCC"))
{
m=2;
if(SBtemp2.name2.equals("BBBB"))
{
n=1;
Matrix[m][n]=SBtemp2.diff;
}
else if(SBtemp2.name2.equals("AAAA"))
{
n=0;
Matrix[m][n]=SBtemp2.diff;
}
}
}
// Print matrix
for(n=0;n<3;n++)
{
for(m=0;m<3;m++)
System.out.print(Matrix[m][n]+" ");
System.out.println(" ");
}
}
}