質問

I'm trying to initialize a new instance of the ArrayList defined in my playingCard.java file:

import java.util.ArrayList;

public class PlayingCard 
{

    public enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
        Jack, Queen, King, Ace}

    public enum Suit { Spades, Diamonds, Hearts, Clubs }

    private final Value value;

    private final Suit suit;

    /**
    * Constructs a card with specified initial value and suit
    * @param value
    * @param suit 
    */

    public PlayingCard(Value value, Suit suit) 
    {
        this.value = value;
        this.suit = suit;
    }

    /**
     * Retrieves the value of a card
     * @return value
     */

    public Value getValue() 
    { 
        return value; 
    }

    /**
     * Retrieves the suit of the card
     * @return suit
     */

    public Suit getSuit() 
    { 
    return suit; 
    }

    /**
    * Custom toString
    *@return value and suit as a string.
    */

    @Override
    public String toString() 
    { 
        return "PlayingCard[value=" + value + ", suit=" + suit + "]"; 
    }

    /**
     * Format method to print out the value and suit of a card.
     * @return value and suit as a string.
     */

    public String format()
    {
        return value + " of " + suit + ", ";
    }

    /*private static final List<PlayingCard> deck = new ArrayList<PlayingCard>();

    // Initialize deck
    static 
    {
        for (Suit suit : Suit.values())
        {
            for (Value value : Value.values())
            {
                 deck.add(new PlayingCard(value, suit));
            }
        }
    }*/
}

If the last 12 or so lines aren't commented out, there is no problem with the code. However I want to initialize the deck in a separate test driver and receive 2 errors when copying the code over. The test driver currently looks like this:

import java.util.ArrayList;

public class PlayingCardTester 
{
public static void main (String[] args)
{
    static  List<PlayingCard> deck = 
        new ArrayList<PlayingCard>();

    // Initialize deck
    static 
    {
        //for ea PlayingCard.Suit suit in PlayingCard.Suit.values()
        for (PlayingCard.Suit suit : PlayingCard.Suit.values())
        {
            for (PlayingCard.Value value : PlayingCard.Value.values())
            {
                deck.add(new PlayingCard(value, suit));
            }
        }

    }
}
}

I have an error on line 14 of the test driver

static  List<PlayingCard> deck = new ArrayList<PlayingCard>();

saying it's an illegal start of expression. I've tried using different keywords in front of the statement and the error stays the same. The second error is the last bracket which just says "null". I am new to using enums, so it could be something very simple which I've over looked...

役に立ちましたか?

解決

You don't need static declaration in static method.

List<PlayingCard> deck = new ArrayList<PlayingCard>();

Also there is no need of Static Block since you are already in static context.


References:

  1. Static Initialization Blocks

他のヒント

In PlayingCardTester You should define

static  List<PlayingCard> deck = 

outside of the main method: one line above "public static void main"

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.

Link

You can not put static variable or block inside method. Both should be outside of main method.

public class PlayingCardTester 
{
    static  List<PlayingCard> deck = new ArrayList<PlayingCard>();

    // Initialize deck
    static 
    {
        //for ea PlayingCard.Suit suit in PlayingCard.Suit.values()
        for (PlayingCard.Suit suit : PlayingCard.Suit.values())
        {
            for (PlayingCard.Value value : PlayingCard.Value.values())
            {
                deck.add(new PlayingCard(value, suit));
            }
        }

    }
    public static void main (String[] args)
    {
        ...
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top