作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前使用 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/
我是一名优秀的程序员,十分优秀!