2012-12-05 35 views
0

我試圖初始化我playingCard.java文件中定義的ArrayList中的新實例:在測試驅動程序初始化ArrayList的

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)); 
      } 
     } 
    }*/ 
} 

如果最後12行左右沒有被註釋掉,有代碼沒有問題。但是,我想在單獨的測試驅動程序中初始化卡片組,並在複製代碼時收到2個錯誤。 測試驅動程序目前看起來是這樣的:

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)); 
      } 
     } 

    } 
} 
} 

我對測試車手

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

的第14行錯誤說這是表達的非法啓動。我試過在聲明前使用不同的關鍵字,錯誤保持不變。 第二個錯誤是剛剛說「null」的最後一個括號。 我是使用枚舉的新手,所以它可能是一個非常簡單的東西,我已經看過...

回答

2

你不需要在靜態方法中聲明static

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

也不需要Static Block因爲您已經處於靜態環境中。


參考文獻:

  1. Static Initialization Blocks
+0

非常感謝你。我也必須導入java.util.List;並且您提供的鏈接非常有用。 –

0

注:這是沒有必要在類定義的開始申報領域,雖然這是最常見的做法。只有在使用它們之前聲明和初始化纔是必要的。

Link

你不能把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) 
    { 
     ... 
    } 
} 
0

在PlayingCardTester你應該定義

static List<PlayingCard> deck = 

的主要方法之外:一個線條以上「公共靜態無效的主要」