gpt4 book ai didi

java编码问题: hangs on a arraylist add

转载 作者:行者123 更新时间:2023-12-02 08:23:53 24 4
gpt4 key购买 nike

我之前使用 arraylist 作为结构,但在这段代码中它不起作用。由于我找不到错误,有人可以帮助我吗? (我确信这是我的错误,但 IDE 没有说什么)

流程:首先是类(class)游戏。我调用 runGame 并且它运行正常,直到 Hand hand = new Hand(this.deck); (右侧有一条注释来表明问题

public class Game {

private ArrayList<Player> playerArray;
private int maxPlayers;
private Deck deck;

//constructor
public Game(ArrayList playerArray, int maxPlayers)
{
this.playerArray = playerArray;
this.maxPlayers = maxPlayers;
}

// game method for the match
public void runGame()
{
//shuffle of players
Collections.shuffle(this.playerArray);

//creation of the deck
this.deck = new Deck();
System.out.println(new java.util.Date().toString() +" "+"deck created");

//shuffle the deck
this.deck.shuffleDeck();
System.out.println(new java.util.Date().toString() +" "+"deck shuffled");


// distribuiting the hands to all players
//and preventing them to send something
for (int i = 0; i < this.maxPlayers; i++)
{
Player currentPlayer = this.playerArray.get(i);
Socket socket = currentPlayer.getConnection();

Hand hand = new Hand(this.deck);// the problem starts here comes form the constructor of the hand
System.out.println(" after hand ");

sendingBlockString(socket, currentPlayer); //send the block string
sendingHand(socket, currentPlayer, hand );//send the hand
}

问题显然出在 Hand 类的 hand 构造函数中,它卡在循环中,exaclty 试图添加甲板上弹出的汽车( Deck.popCard() 函数经过测试并且工作完美,所以它不是阻止 add() 函数)我永远不会到达第二个 system.out.println 这里的代码:

public class Hand  implements Serializable
{

private ArrayList<Card> theHand;
private Player player;
private int handValue ; // from 1 to 10


public Hand(Deck deck)
{
for (int i=0; i<4; i++)
{
System.out.println("before popping deck");
this.theHand.add(i, deck.popCard());// adding the card taken from the deck (5 times) //// this is the problem it hangs!
System.out.println("after add to hand");
}

}

最佳答案

你确定它挂起了吗?它应该抛出 NullPointerException,因为您的 ArrayList 尚未初始化。

关于java编码问题: hangs on a arraylist add,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com