我试图在ArrayLists的ArrayList中添加一个特定的成员

时间:2016-06-29 15:10:41

标签: java arraylist

我试图将扑克牌添加到玩家卡片列表中(玩家0和玩家1)。我试图成为通用的,并且为n个玩家的游戏实现易于更改的代码。但是,我对语法感到困惑,为特定播放器添加新卡片并不起作用...请帮忙:

public class GameThread extends Thread{
private Socket socket = null;
private Socket[] connections = new Socket[2];
private List<List<Card>> player = new ArrayList<List<Card>>();

CardPack cardPack = new CardPack(); //creating a new card pack

public GameThread(int sessionID, Socket s) {
    socket = s;
//      Initial card for two players
    for (int i = 0; i < 2; i++){
        player.add(0, this.cardPack.getRandomCard());
        player.add(1, this.cardPack.getRandomCard());

    }
}

PS。这是具体答案的具体问题。感谢Sotirios Delimanolis,Tunaki帮助我签署我的问题作为重复问题将某些人从SQL转移到ArrayLists,但为什么要改变声誉?

用于地狱火箭的Real T H A N K S

1 个答案:

答案 0 :(得分:0)

player.get(0).add(cardPack.getRandomCard());
player.get(1).add(cardPack.getRandomCard());

我建议将这两个玩家命名为将来参考,因为你可能需要重复使用它们。

public class GameThread extends Thread{
private Socket socket = null;
private Socket[] connections = new Socket[2];
private List<List<Card>> players = new ArrayList<List<Card>>();

CardPack cardPack = new CardPack(); //creating a new card pack

public GameThread(int sessionID, Socket s) {
    socket = s;
//      Initial card for two players
    List<Card> playerOne = new ArrayList<>();
    List<Card> playerTwo = new ArrayList<>();
    players.add(playerOne);
    players.add(playerTwo);
    playerOne.add(this.cardPack.getRandomCard());
    playerTwo.add(this.cardPack.getRandomCard());
    }
}