如何在Java中初始化对象数组
我想初始化 BlackJack 游戏的 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;
}
编辑 - 更新: 以下是我理解您的建议后更改此内容后得到的结果:
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
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
几乎没问题了。只需:
并让循环为:
并注意 java 约定规定方法和变量的名称应以小写字母开头。
更新:将您的方法放在类主体中。
It is almost fine. Just have:
And let the loop be:
And note that java convention dictates that names of methods and variables should start with lower-case.
Update: put your method within the class body.
而不是
您想要的
,
应该返回用 Player 实例初始化的数组。
编辑:
请查看 维基百科上的此表,了解 java 的命名约定被广泛使用。
Instead of
you want
and
should return the array initialized with Player instances.
EDIT:
Do check out this table on wikipedia on naming conventions for java that is widely used.
如果您不确定数组的大小或者它是否可以更改,您可以这样做以获得静态数组。
If you are unsure of the size of the array or if it can change you can do this to have a static array.
如果你可以硬编码玩家数量
If you can hard-code the number of players
数组在初始化后不可更改。您必须给它一个值,该值就是数组长度所保留的值。您可以创建多个数组来包含玩家信息的某些部分(例如手牌等),然后创建一个 arrayList 来对这些数组进行排序。
我看到的另一个争论点是,您的 private Player[] InitializePlayers() 是静态的,而该类现在是非静态的。所以:
我的最后一点是,您可能应该在要更改它的方法之外声明playerCount,以便设置给它的值也成为新值,并且它不只是在结束时被丢弃该方法的“范围”。
希望这有帮助
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.
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:
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."
Hope this helps
thePlayers[i] = new Player(i);
我刚刚删除了Player(i)
中的i
;它起作用了。所以代码行应该是:
thePlayers[i] = new Player(i);
I just deleted thei
insidePlayer(i)
; and it worked.so the code line should be: