如何使用ArrayList双输入法?

时间:2014-12-02 09:39:02

标签: java arraylist

我有5张牌ArrayList,我需要修改我的 addCard方法,以便查找我手中的第一张卡,其值小于卡片c。然后我必须使用void add( int index, E x )并插入我手中的卡片c。

getValue()是一种查找卡片点值的方法

private ArrayList<Card> myHand;是一个ArrayList,而Card是一个类。

我对如何使用添加方法感到困惑,这是我的尝试。

// Insert definitions for the getCardValue and addCard methods here
public int getCardValue(int n) {
    return myHand.get(n).getValue();
}

public void addCard(Card c) {
    for (int i = 0; i < 5; i++)
        while (myHand.get(i).getValue() < myHand.get(c).getValue())
            myHand.add(i, c);
}

public static void main(String[] args) {
    Card[] cards = { new Card("spades", 4), new Card("hearts", 10),
                    new Card("clubs", 12), new Card("diamonds", 14),
                    new Card("diamonds", 2) };
    Hand h = new Hand();
    for (int i = 0; i < 5; i++)
        h.addCard(cards[i]);
    System.out.println(h.getCardValue(3));
}

谢谢!

0 个答案:

没有答案