如何在Java中初始化对象数组

时间:2022-09-25 12:58:54

I want to initialize an array of Player objects for a BlackJack game. I've read a lot about various ways to initialize primitive objects like an array of ints or an array of strings but I cannot take the concept to what I am trying to do here (see below). I would like to return an array of initialized Player objects. The number of player objects to create is an integer for which I prompt the user. I was thinking the constructor could accept an integer value and name the player accordingly while initializing some member variables of the Player object. I think I am close but still quite confused too.

我想要为一个BlackJack游戏初始化一个玩家对象的数组。我已经读了很多关于初始化对象的方法,比如数组ints或字符串数组,但是我不能把这个概念应用到这里(参见下面)。我想返回一个初始化的Player对象数组。要创建的player对象的数量是我提示用户的整数。我认为构造函数可以接受一个整数值,并在初始化player对象的一些成员变量时相应地命名player。我想我很接近了,但还是很困惑。

static class Player
{
    private String Name;
    private int handValue;
    private boolean BlackJack;
    private TheCard[] Hand;

    public Player(int i)
    {
        if (i == 0)
        {
            this.Name = "Dealer"; 
        }
        else
        {
            this.Name = "Player_" + String.valueOf(i);
        }
        this.handValue = 0;
        this.BlackJack = false;
        this.Hand = new TheCard[2];
    } 
}
private static Player[] InitializePlayers(int PlayerCount)
{ //The line below never completes after applying the suggested change
    Player[PlayerCount] thePlayers;
    for(int i = 0; i < PlayerCount + 1; i++)
    {
        thePlayers[i] = new Player(i);
    }
    return thePlayers;
}

EDIT - UPDATE: Here is what I am getting after changing this as I understood your suggestion:

编辑-更新:以下是我理解你的建议后得到的结果:

Thread [main] (Suspended)   
    ClassNotFoundException(Throwable).<init>(String, Throwable) line: 217   
    ClassNotFoundException(Exception).<init>(String, Throwable) line: not available 
    ClassNotFoundException.<init>(String) line: not available   
    URLClassLoader$1.run() line: not available  
    AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]   
    Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available   
    Launcher$ExtClassLoader.findClass(String) line: not available   
    Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
    Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
    Launcher$AppClassLoader.loadClass(String, boolean) line: not available  
    Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available  
    BlackJackCardGame.InitializePlayers(int) line: 30   
    BlackJackCardGame.main(String[]) line: 249  

5 个解决方案

#1


78  

It is almost fine. Just have:

几乎好了。只有:

Player[] thePlayers = new Player[playerCount + 1];

And let the loop be:

让循环为:

for(int i = 0; i < thePlayers.length; i++)

And note that java convention dictates that names of methods and variables should start with lower-case.

注意,java惯例规定方法和变量的名称应该以小写开头。

Update: put your method within the class body.

更新:将方法放在类体中。

#2


17  

Instead of

而不是

Player[PlayerCount] thePlayers;

you want

你想要的

Player[] thePlayers = new Player[PlayerCount];

and

for(int i = 0; i < PlayerCount ; i++)
{
    thePlayers[i] = new Player(i);
}
return thePlayers;

should return the array initialized with Player instances.

应该返回用Player实例初始化的数组。

EDIT:

编辑:

Do check out this table on wikipedia on naming conventions for java that is widely used.

请查看wikipedia上的这个表,了解广泛使用的java的命名约定。

#3


12  

If you are unsure of the size of the array or if it can change you can use this conversation to have a static array.

如果您不确定数组的大小,或者它是否可以更改,您可以使用这个对话拥有一个静态数组。

ArrayList<Player> thePlayersList = new ArrayList<Player>(); 

thePlayersList.add(new Player(1));
thePlayersList.add(new Player(2));
.
.
//Some code here that changes the number of players e.g

Players[] thePlayers = thePlayersList.toArray();

#4


1  

Arrays are not changeable after initialization. You have to give it a value, and that value is what that array length stays. You can create multiple arrays to contain certain parts of player information like their hand and such, and then create an arrayList to sort of shepherd those arrays.

数组在初始化后不会改变。你必须给它一个值,这个值就是数组长度的值。您可以创建多个数组来包含玩家信息的某些部分,比如他们的手等等,然后创建一个arrayList来引导这些数组。

Another point of contention I see, and I may be wrong about this, is the fact that your private Player[] InitializePlayers() is static where the class is now non-static. So:

我看到的另一个争论点是,您的私有播放器[]InitializePlayers()是静态的,而类现在是非静态的。所以:

private Player[] InitializePlayers(int playerCount)
{
 ...
}

My last point would be that you should probably have playerCount declared outside of the method that is going to change it so that the value that is set to it becomes the new value as well and it is not just tossed away at the end of the method's "scope."

我的最后一点是,你应该有playerCount外声明的方法去改变它,将它的值变成了新的价值,不仅仅是扔掉的方法的“范围”。

Hope this helps

希望这有助于

#5


0  

thePlayers[i] = new Player(i); I just deleted the i inside Player(i); and it worked.

球员[我]=新球员(我);我刚刚删除了我的内部播放器(I);它工作。

so the code line should be:

因此代码行应该是:

thePlayers[i] = new Player9();

#1


78  

It is almost fine. Just have:

几乎好了。只有:

Player[] thePlayers = new Player[playerCount + 1];

And let the loop be:

让循环为:

for(int i = 0; i < thePlayers.length; i++)

And note that java convention dictates that names of methods and variables should start with lower-case.

注意,java惯例规定方法和变量的名称应该以小写开头。

Update: put your method within the class body.

更新:将方法放在类体中。

#2


17  

Instead of

而不是

Player[PlayerCount] thePlayers;

you want

你想要的

Player[] thePlayers = new Player[PlayerCount];

and

for(int i = 0; i < PlayerCount ; i++)
{
    thePlayers[i] = new Player(i);
}
return thePlayers;

should return the array initialized with Player instances.

应该返回用Player实例初始化的数组。

EDIT:

编辑:

Do check out this table on wikipedia on naming conventions for java that is widely used.

请查看wikipedia上的这个表,了解广泛使用的java的命名约定。

#3


12  

If you are unsure of the size of the array or if it can change you can use this conversation to have a static array.

如果您不确定数组的大小,或者它是否可以更改,您可以使用这个对话拥有一个静态数组。

ArrayList<Player> thePlayersList = new ArrayList<Player>(); 

thePlayersList.add(new Player(1));
thePlayersList.add(new Player(2));
.
.
//Some code here that changes the number of players e.g

Players[] thePlayers = thePlayersList.toArray();

#4


1  

Arrays are not changeable after initialization. You have to give it a value, and that value is what that array length stays. You can create multiple arrays to contain certain parts of player information like their hand and such, and then create an arrayList to sort of shepherd those arrays.

数组在初始化后不会改变。你必须给它一个值,这个值就是数组长度的值。您可以创建多个数组来包含玩家信息的某些部分,比如他们的手等等,然后创建一个arrayList来引导这些数组。

Another point of contention I see, and I may be wrong about this, is the fact that your private Player[] InitializePlayers() is static where the class is now non-static. So:

我看到的另一个争论点是,您的私有播放器[]InitializePlayers()是静态的,而类现在是非静态的。所以:

private Player[] InitializePlayers(int playerCount)
{
 ...
}

My last point would be that you should probably have playerCount declared outside of the method that is going to change it so that the value that is set to it becomes the new value as well and it is not just tossed away at the end of the method's "scope."

我的最后一点是,你应该有playerCount外声明的方法去改变它,将它的值变成了新的价值,不仅仅是扔掉的方法的“范围”。

Hope this helps

希望这有助于

#5


0  

thePlayers[i] = new Player(i); I just deleted the i inside Player(i); and it worked.

球员[我]=新球员(我);我刚刚删除了我的内部播放器(I);它工作。

so the code line should be:

因此代码行应该是:

thePlayers[i] = new Player9();