简体   繁体   English

如何打印输入的ArrayList

[英]How to print out inputted ArrayList

I'm trying to print out what I inputted for my ArrayList but I keep getting weird results such as this [player@42a57993, player@75b84c92, player@6bc7c054] when I clearly put in legitimate names. 我正在尝试打印我为ArrayList输入的内容,但是当我明确输入合法名称时[player@42a57993, player@75b84c92, player@6bc7c054]我会不断收到诸如[player@42a57993, player@75b84c92, player@6bc7c054]类的奇怪结果。 So take a look at my code and see what the problem might be: 因此,请看一下我的代码,看看可能是什么问题:

if (o == 5) {
    double RD, t, old, x;
    String tournament, player;
    int a, number_of_players, place;
    ArrayList<player> players = new ArrayList<player>();
    System.out.println("1:Add a tournament \t2:View Existing");
    a = keyIn.nextInt();
        if (a == 1) {
            System.out.println("\nEnter tournament name");
            tournament = keyIn.next();
            System.out.println("\nEnter number of players");
            number_of_players = keyIn.nextInt();
            System.out.println("Enter players");
            for (int i = 0; i < number_of_players; i++) {
                String name = keyIn.next();
                player plr = new player();
                plr.setName(name);
                players.add(plr);
            }
            System.out.println("Enter places for");
            System.out.println(players);
            place = keyIn.nextInt();

Here is my player class: 这是我的player课:

public class player {

    private static String name;

    public void setName(String pName)
    {
        name = pName;
    }

    public String getName()
    {
        return name;
    }
}

Let me know what you guys come up with! 让我知道你们的想法! Thanks! 谢谢!

What you see is the output of the default implementation of toString . 您将看到toString的默认实现的输出。 It's the class name followed by @ and the unsigned hexadecimal representation of the hash code. 它是类名,后跟@和哈希码的无符号十六进制表示形式。

To change that, implement a toString method in your Player class, for example: 要更改此设置,请在Player类中实现toString方法,例如:

public class Player {

    private String name;

    public void setName(String pName) {
        name = pName;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Player{name='" + name + "'}";
    }
}

Java has no clue about local variable names after the item is added. 添加项目后,Java不了解局部变量名称。 When you run Collections.toString , it will simply call the toString method of all instances in the ArrayList . 当您运行Collections.toString ,它将仅调用ArrayList中所有实例的toString方法。

You should override the toString method: 您应该重写toString方法:

public class player {

    private static String name;

    public void setName(String pName) {
        name = pName;
    }

    public String getName() {

        return name;
    }

    @Override
    public String toString () {
        return name;
    }

}

The toString method specifies a textual representation of Player instances. toString方法指定Player实例的文本表示形式。 The default toString method is printing the class of the instance as well as its location in memory. 默认的toString方法将打印实例的类及其在内存中的位置。

There are of course alternative ways to produce a textual representation. 当然,存在产生文本表示的替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM