Cómo inicializar una matriz de objetos en Java
Quiero inicializar una matriz de objetos Player para un juego de BlackJack. He leído mucho sobre varias formas de inicializar objetos primitivos como una matriz de entradas o una serie de cadenas, pero no puedo llevar el concepto a lo que estoy tratando de hacer aquí (ver más abajo). Me gustaría devolver una matriz de objetos Player inicializados. El número de objetos de jugador para crear es un número entero para el que le pido al usuario. Estaba pensando que el constructor podría aceptar un valor entero y nombrar al jugador en consecuencia al inicializar algunas variables miembro del objeto Player. Creo que estoy cerca pero también bastante confundido.
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 - ACTUALIZACIÓN: Esto es lo que obtengo después de cambiar esto, ya que entendí tu sugerencia:
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