0

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...

4

3 回答 3

2

您不需要static在静态方法中声明。

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

Static Block由于您已经处于静态上下文中,因此也不需要。


参考:

  1. Static Initialization Blocks
于 2012-12-05T17:38:12.297 回答
0

在 PlayingCardTester 你应该定义

static  List<PlayingCard> deck = 

主方法之外:“public static void main”上方的一行

于 2012-12-05T17:41:17.760 回答
0

注意:没有必要在类定义的开头声明字段,尽管这是最常见的做法。只需要在使用它们之前声明和初始化它们。

关联

你不能把static variable or block里面的方法。两者都应该在main方法之外。

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)
    {
        ...
    }
}
于 2012-12-05T17:38:53.143 回答