使用deepToString在二维数组中打印对象

时间:2014-07-25 12:00:50

标签: java arrays multidimensional-array

我正在尝试编写一个Java应用程序来模拟足球场(以世界杯的风格),并随机生成比赛报告和结果。由于我想为每个团队分配球员,我需要团队成为对象而不仅仅是字符串。由于我在运行应用程序时努力打印完整的团队列表,这证明是有问题的。到目前为止,我有团队课程:

import java.util.Arrays;

public class Team {

public static Team Team1 = new Team();
public static Team Team2 = new Team();
public static Team Team3 = new Team();
...
public static Team Team16 = new Team();

public static Team[][] FullTeamList = {
    {Team1, Team2, Team3, Team4},
    {Team5, Team6, Team7, Team8},
    {Team9, Team10, Team11, Team12},
    {Team13, Team14, Team15, Team16}
};

public static void printFullTeamList(){
    System.out.println(Arrays.deepToString(FullTeamList));
}
}

(在某些时候我会手动更改团队名称,这就是我选择不使用循环来实例化团队的原因)

主要方法是:

import java.util.Arrays;

public class Main {
    public static void main (String[] args){
        Team.printFullTeamList();
    }
}

不幸的是,当我运行该程序时,它返回:

[[Team@25d35bf2, Team@57398044, Team@141d19, Team@28825459], [Team@4187a8e0, ...

但如果可能,我希望它能够回归:

[[Team1, Team2, Team3, Team4, Team5, ... etc

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您需要覆盖Team类中的toString方法。更多详情:http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

另外,我认为Team1,...... Team16应放在其他地方。