关于.add()的ArrayList错误

时间:2015-05-04 07:08:06

标签: java arraylist

非常感谢任何帮助。我一直在摸不着头脑,不知道为什么我的ArrayLists都不喜欢.add()。错误在名为play的方法中,在名为SimplePoker的类中。我也在这里玩弄另外两个名为Decks and Card的课程,所以我觉得我的问题与此有关,但我现在还不完全确定:

public void play() 
{
    //Array lists
    ArrayList<Card>currentHand = new ArrayList<Card>();
    ArrayList<Decks> dealCards = new ArrayList<Decks>();

    // implement this method!
    while(balance >0){


        //Showing the balance
        balance = startingBalance;
        System.out.println("Your current balance is: " +balance);

        //Scanner asking for wager
        Scanner fromKeyboard = new Scanner(System.in);
        System.out.println("How much would you like to wager? ");
        bet = fromKeyboard.nextInt();

        //To play
        if(bet <= balance){
            Scanner in = new Scanner(System.in);
            List<Card> discardCards;
            System.out.println("Lets play!");

            //Placing bet
            balance -= bet;

            //Distributing first 5 cards
            for(int i=0; i<5; i++){
                currentHand.add(dealCards.get(i));
                dealCards.remove(i);

            }

            //Discarding cards
            System.out.println("Which cards would you like to throw (Enter any of the digits: 1 2 3 4 5, respectively: ");
            while (in.hasNext()){
                discardCards.add(in);
            }

        }
        //Or not to play
        else
            System.out.println("Not a valid wager");

    }

}

当我运行它时,我收到此错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method add(Card) in the type List<Card> is not applicable for the arguments (Decks)
The method add(Card) in the type List<Card> is not applicable for the arguments (Scanner)

2 个答案:

答案 0 :(得分:4)

您的阵列有不同的类型。

ArrayList<Decks> dealCards = new ArrayList<Decks>();
ArrayList<Card>currentHand = new ArrayList<Card>();
List<Card> discardCards;

currentHand.add(dealCards.get(I)); - 您无法将Deck对象添加到卡阵列

同样在这里:

 while (in.hasNext()){
            discardCards.add(in);
        }

你无法在&#34;中添加&#34;到列表。您需要阅读&#34; in&#34;并将值转换为对象。

答案 1 :(得分:1)

dealCards.get(i)返回Decks的实例。 因此无法将currentHand添加到cards预期Client> GET: /Federation/Sources Server>> {"sessionId":xyz,"data":{"source1"...........}} Client> GET: /Federation/Sources/{sessionId} Server>> {"sessionId":xyz,"data":{"sourceadded"...........}} Client> PUT: /Federation/Sources/{sessionId} {"data":{"Recieved"}}

的实例