字符串变量突然变成字母和数字的组合

Com*_*erd 0 java string jlabel

我有一个JLabel PlayerLabel数组和PlayerName数组,它是以下列方式初始化的

        String [] PlayerName;
        PlayerName = new String[4];
        for (int i=0;i<4;i++)
        {
            PlayerName[i] = "None1";

        }

JLabel [] PlayerLabel;

PlayerLabel = new JLabel[4];

        for (int i=0;i<4;i++)
        {
            String num = Integer.toString(i);
            String output = "Player " + num + " : " + PlayerName;

            PlayerLabel[i] = new JLabel(output);
            PlayerLabel[i].setForeground(Color.white);

        }
Run Code Online (Sandbox Code Playgroud)

我希望我的JLabel的文本是

玩家1:无1

相反,我得到了

玩家1:java.lang.String; @ 4e9fd887

@15150ef8每次重新启动程序时,部件都会更改为不同的数字和字母组合.

当我初始化时,PlayerName没问题 在此输入图像描述

我不知道为什么在输出时添加PlayerName会变得怪异

在此输入图像描述

发生了什么,我该如何解决这个问题?

awk*_*ksp 10

当你这样做时+ Playername,你打印的是数组本身,而不是数组中的元素.当您尝试将对象连接到字符串时,将toString()调用该对象的方法,并且数组继承toString()Object:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Run Code Online (Sandbox Code Playgroud)

这可能不是你想要的.

你可能想要这样做Playername[i].