如何初始化Java对象的数组 [英] How to initialize an array of objects in Java

查看:153
本文介绍了如何初始化Java对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要初始化二十一点游戏玩家对象的数组。我读过很多关于各种方法来初始化基本对象像int数组或字符串数​​组,但我不能拿这个概念是什么,我想在这里做(见下文)。我想回到初始化玩家对象的数组。创建播放对象的数目为我提示用户的整数。我想构造可以接受一个整数值,并依此命名的球员在初始化玩家对象的某些成员变量。我想我接近,但还是挺迷茫了。

 静态类播放器
{
    私人字符串名称;
    私人诠释handValue;
    私人布尔的BlackJack;
    私人TheCard []手;    公众播放器(int i)以
    {
        如果(我== 0)
        {
            this.Name =经销商;
        }
        其他
        {
            this.Name =Player_+将String.valueOf(ⅰ);
        }
        this.handValue = 0;
        this.BlackJack = FALSE;
        this.Hand =新TheCard [2];
    }
}
私有静态播放器[] InitializePlayers(INT PlayerCount)
{//下面一行30永远不会完成
    播放器[] = thePlayers新的播放器[PlayerCount + 1]; //行30%的建议改变
    的for(int i = 0; I< PlayerCount + 1;我++)
    {
        thePlayers [I] =新播放器(ⅰ);
    }
    返回thePlayers;
}

修改 - 更新:
下面是我改变这之后得到我的理解你的建议:

 发[主](暂停)
    。ClassNotFoundException异常(可抛出)LT; INIT>(字符串,可抛出)线:217
    。ClassNotFoundException的(例外)LT; INIT>(字符串,可抛出)行:不可用
    。ClassNotFoundException的<&初始化GT;(字符串)行:不可用
    URLClassLoader的$ 1.run()行:不可用
    在AccessController.doPrivileged(的PrivilegedExceptionAction< T&GT ;,的AccessControlContext)行:不可用[本机方法]
    启动$的ExtClassLoader(的URLClassLoader).findClass(字符串)行:不可用
    启动$ ExtClassLoader.findClass(字符串)行:不可用
    启动$的ExtClassLoader(ClassLoader的).loadClass(字符串,布尔)行:不可用
    启动$ AppClassLoader器(classloader).loadClass(字符串,布尔)行:不可用
    启动$ AppClassLoader.loadClass(字符串,布尔)行:不可用
    启动$ AppClassLoader器(classloader).loadClass(字符串)行:不可用
    BlackJackCardGame.InitializePlayers(INT)行:30
    BlackJackCardGame.main(字符串[])线:249


解决方案

这几乎是罚款。只要有:

 播放器[] = thePlayers新的播放器[playerCount + 1];

而让循环是:

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

和注意,Java习惯上还是会把方法和变量名称应该以小写开头。

更新:把你的方法的类体内

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)
{                                                                   // line 30 below never completes
    Player[] thePlayers = new Player[PlayerCount + 1];      // line 30 changed per suggestions
    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  

解决方案

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.

Update: put your method within the class body.

这篇关于如何初始化Java对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆