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

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

问题描述

我想为二十一点游戏初始化一组 Player 对象.我已经阅读了很多关于初始化原始对象(如整数数组或字符串数​​组)的各种方法,但我无法将这个概念用于我在这里尝试做的事情(见下文).我想返回一组初始化的 Player 对象.要创建的玩家对象的数量是我提示用户输入的整数.我认为构造函数可以接受一个整数值并在初始化 Player 对象的一些成员变量时相应地命名播放器.我想我很接近但仍然很困惑.

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  

推荐答案

几乎没问题.只要有:

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

让循环成为:

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

请注意,Java 约定规定方法和变量的名称应以小写开头.

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天全站免登陆