无法转换为链接列表

时间:2014-05-13 23:39:26

标签: java linked-list

所以我正在编写这个二十一点程序,而我却无法将我的类数组转换为链接列表。我真的不知道从哪里开始,任何帮助都会受到赞赏。

这是我的卡类

  public class Card
{

/********* Instance Data **************/

  private String stringFaceValue;  //string face of the card
  private String stringSuitValue;  //string suit of the card
  private int numFaceValue;        //integer face of the card
                                   //ace is 1 and Jack-King is 11-13
  private int numSuitValue;        //integer suit of the card
  private int uniqueID;            //a unique ID of each card

/********** Contructors **************/

    public Card(int inSuit, int inFaceVal)
    {
        numFaceValue = inFaceVal;
        numSuitValue = inSuit;
        stringFaceValue = "";
        uniqueID = 0;

        if(inSuit == 1)
            stringSuitValue = "Hearts";
        else if(inSuit == 2)
            stringSuitValue = "Clubs";
        else if(inSuit == 3)
            stringSuitValue = "Diamonds";
        else
            stringSuitValue = "Spades";
    }

    /************ Methods ***************/
    public String toString()
    {
        //local constants

        //local variables
        String format;    //formatted string to be returned

        /********** begin toSTring ***************/

        //check which face value to return
        switch(numFaceValue)
        {
        case 1:
            stringFaceValue = "Ace";
            break;

        case 2:
            stringFaceValue = "Two";
            break;

        case 3:
            stringFaceValue = "Three";
            break;

        case 4:
            stringFaceValue = "Four";
            break;

        case 5:
            stringFaceValue = "Five";
            break;

        case 6:
            stringFaceValue = "Six";
            break;

        case 7:
            stringFaceValue = "Seven";
            break;

        case 8:
            stringFaceValue = "Eight";
            break;

        case 9:
            stringFaceValue = "Nine";
            break;

        case 10:
             stringFaceValue = "Ten";
             break;

        case 11:
             stringFaceValue = "Jack";
             break;

        case 12:
             stringFaceValue = "Queen";
             break;

        case 13:
             stringFaceValue = "King";
             break;
        }//end switch

        //format the string
        format = stringFaceValue + " of " + stringSuitValue;

        //return the string
        return format;
    }

    public int getNumber()
    {
        return numFaceValue;
    }



} // end class Card'

我的DeckofCards课程

import java.lang.*;
import java.text.*;
import java.util.*;

public class DeckofCards
{

    private static Card[] cards;//the array of cards
    private static int numCards;//the number of cards in the deck

    public DeckofCards(boolean shuffle)
    {
        numCards = 52;
        cards = new Card[numCards];

        int cardIndex = 0;

        //for each suit of cards
        for(int suit = 0; suit < 4; suit++)
        {
            for(int faceVal = 1; faceVal <= 13; faceVal ++)
            {
                //add a new card to deck
                cards[cardIndex] = new Card(suit, faceVal);
                cardIndex++;
            }
        }

        //if the user wants to shuff
        if(shuffle == true)
        {
            DeckofCards.shuffle();
        }
    }//end constructor

    public static void shuffle()
    {
        //initialize random number generator
        Random shuffleDeck = new Random();

        //temporary card value
        Card temp;

        int tempIndex;

        for(int i = 0; i < numCards; i++)
        {
            //get a random card to swap
            tempIndex = shuffleDeck.nextInt(numCards);

            //swap the cards
            temp = cards[i];
            cards[i] = cards[tempIndex];
            cards[tempIndex] = temp;
        }
    }//end shuffle

    public Card dealNextCard()
    {
        //get the top card
        Card top = cards[0];

        for(int c = 1; c < numCards; c++)
        {
            cards[c - 1] = cards[c];
        }
        cards[numCards - 1] = null;
        numCards--;
        return top;
    }//end dealNextCard

    public void printDeck()
    {
        for(int c = 0; c < numCards; c++)
        {
            System.out.println(cards[c]);
        }
    }//end printDeck
}

我的玩家手类

public class Hand
{
    private Card[] hand = new Card[10];
    private int numCards;

    public Hand()
    {
        //set player hand to empty
        this.emptyHand();

    }//end constructor

    public void emptyHand()
    {
        for(int c = 0; c < 10; c++)
        {
            hand[c] = null;
        }

        numCards = 0;
    }//end emptyHand

    public boolean addCard(Card aCard)
    {
        hand[numCards] = aCard;
        numCards++;

        return (this.getHandSum() <= 21);
    }//end addCard

    public int getHandSum()
    {
        int handSum = 0;
        int numAces = 0;
        int cardNum;

        //calc the total
        for(int c = 0; c < numCards; c++)
        {
            cardNum = this.hand[c].getNumber();

            if(cardNum == 1)
            {
                numAces++;
                handSum+= 11;
            }
            else if(cardNum > 10)
            {
            handSum += 10;
            }
            else
                handSum += cardNum;
        }//end for calcTotal

            //if we have aces that bust the total
            while(handSum > 21 && numAces > 0)
            {
                handSum -= 10;
                numAces --;
            }

        return handSum;
    }//end getHandsum

    public void printHand()
    {
        for(int c = 0; c < numCards; c++)
        {
            System.out.println("\t" + hand[c].toString());
        }
    }//end printHand
}

和我的司机

import java.util.*;
import java.text.*;
import java.lang.*;

public class Game
{
    public static void main(String[]args)
    {
        //local constants

        //local variables
        boolean turnDone = false;      //sees if the user wants to hit
        String toPlay;
        int sum;
        String answer;
        DeckofCards theDeck = new DeckofCards(true);
        Hand myHand = new Hand();


        /************ begin main *****************/

        //show startup
        System.out.println("\n\n************************");
        System.out.println("* WELCOME TO BLACKJACK *");
        System.out.println("************************\n\n");

        //prompt the user if they want to play again
        System.out.print("Do you want to play?(Yes or No):  ");
        toPlay = Keyboard.readString();

        while(toPlay.equalsIgnoreCase("yes"))
        {
            System.out.print("\n");

            //deal yourself your hand
            myHand.addCard(theDeck.dealNextCard());
            myHand.addCard(theDeck.dealNextCard());

            //print your hand
            System.out.println("Your Cards Were Dealt\n");
            myHand.printHand();
            System.out.println("\n");

            //while I want to get a card
            while(!turnDone)
            {
                System.out.print("Do you want to hit?:  ");
                answer = Keyboard.readString();
                System.out.print("\n");

                if(answer.equalsIgnoreCase("yes"))
                {
                    //add the next card and see if busto
                    turnDone = !myHand.addCard(theDeck.dealNextCard());
                    myHand.printHand();
                    System.out.print("\n");
                }
                else
                {
                    turnDone = true;
                }
            }//end while turn isn't done

            //display the hand
            sum = myHand.getHandSum();

            if(sum <= 21)
               System.out.println("You win!!\n");
            else
               System.out.println("You busted!!\n");

            //show startup
            System.out.println("\n\n************************");
            System.out.println("* WELCOME TO BLACKJACK *");
            System.out.println("************************\n\n");

            //prompt the user if they want to play again
            System.out.println("Do you want to play?");
            toPlay = Keyboard.readString();

            //empty the hand
            myHand.emptyHand();

            //set the turnboolean to false
            turnDone = false;

        }//end toPlay

    }//end main
}

一切都运行并且编译得很好而且有效。我只是不知道如何将其转换为链表格式

1 个答案:

答案 0 :(得分:0)

http://blog.xeiam.com/blog/2013/07/28/convert-an-array-into-a-linkedlist-in-java/

从链接:

&#34;要将数组转换为java.util.LinkedList,我们可以使用java.util.Arrays类的asList()方法。 java.util包中的Arrays类提供了一个将数组转换为List的实用工具方法。 Arrays.asList()方法将数组转换为固定大小的List。要创建LinkedList,我们只需要将List传递给java.util.LinkedList类的构造函数。也可以用这种方式创建java.util.ArrayList。&#34;