Java에서 객체 배열을 초기화하는 방법
BlackJack 게임을 위해 Player 객체 배열을 초기화하고 싶습니다. int 배열이나 문자열 배열과 같은 기본 객체를 초기화하는 다양한 방법에 대해 많이 읽었지만 여기서 수행하려는 개념을 이해할 수는 없습니다 (아래 참조). 초기화 된 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
거의 괜찮습니다. 그냥 가지고 :
Player[] thePlayers = new Player[playerCount + 1];
그리고 루프를 다음과 같이 두십시오.
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.
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.
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.
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();
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:
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."
Hope this helps
thePlayers[i] = new Player(i);
I just deleted the i
inside Player(i)
; and it worked.
so the code line should be:
thePlayers[i] = new Player9();
참고URL : https://stackoverflow.com/questions/5889034/how-to-initialize-an-array-of-objects-in-java
'Development Tip' 카테고리의 다른 글
Linq to SQL을 사용한 NOLOCK (0) | 2020.11.04 |
---|---|
Android 레이아웃 폴더 : 레이아웃, 레이아웃 포트, 레이아웃 랜드 (0) | 2020.11.04 |
C ++의 #region에 해당 (0) | 2020.11.04 |
gcc는 i686에서 bits / predefs.h를 찾을 수 없습니다. (0) | 2020.11.04 |
파이썬 matplotlib 그림에서 숫자가 지수 형식으로 변경되는 것을 방지하는 방법 (0) | 2020.11.04 |