创建一副牌时的编译错误

时间:2014-03-02 23:04:42

标签: java

我正在尝试创建一副牌,但是我在访问我使用ArrayList.get方法创建的牌组时遇到了一些问题,我不确定我是否已正确创建了牌阵列。

卡类:

package cardgame;

public class Card {

private String number;
private String suit;

public Card(String number, String suit) {
    this.number = number;
    this.suit = suit;
}

public void setNumber(String number) {
    this.number = number;
}

public void setSuite(String suit) {
    this.suit = suit;
}

public String getNumber() {
    return number;
}

public String getSuit() {
    return suit;
}

}

甲板课程:

package cardgame;

import java.util.*;

public class Deck {

private String[] numberList = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] suitList = {"Hearts", "Diamonds", "Clubs", "Spades"};

private ArrayList cardDeck = new ArrayList();

public ArrayList Deck() {
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 13; j++) {
            cardDeck.add(new Card(numberList[j], suitList[i]));
        }
    }
    return cardDeck;
}

}

主要课程:

package cardgame;

public class CardGame {

public static void main(String[] args) {
    Deck newDeck = new Deck();
    System.out.println(newDeck.get(1));
}

}

在主类中,我只是使用newDeck.get来检索数据,以查看是否实际填充了ArrayList。我收到以下错误指向该行:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at cardgame.CardGame.main(CardGame.java:7)
Java Result: 1

3 个答案:

答案 0 :(得分:1)

我会使Card类不可变,因为一旦分配了数字和套装,它们将永远不会改变。或者你曾经在现实生活中玩纸牌游戏吗? 你必须以这两种方式定义这两个参数。

final关键字表示,一旦为变量指定了值,就不能再对其进行更改。您还可以保存几行代码。

以下是Card类:

package cardgame;

public class Card {

private final String number;
private final String suit;

public Card(String number, String suit) {
    this.number = number;
    this.suit = suit;
}


public String getNumber() {
    return number;
}

public String getSuit() {
    return suit;
}

// Convenient for System.out.println()
@Override
public String toString() { return suit + " " + number; }

}

toString()是为每个Object声明的方法,并返回该类的String表示形式。我只是重新声明了这种表示的样子。您可以在下面的主要方法中看到我为什么这样做。

现在到Deck班级:

package cardgame;

import java.util.*;
// The Deck is a list of cards, so let it inherit from AbstractList
// Deck will be a Read-only list with all the syntactic sugar of lists then
public class Deck extends AbstractList {

private String[] numberList = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] suitList = {"Hearts", "Diamonds", "Clubs", "Spades"};

private ArrayList<Card> cardDeck = new ArrayList<>();


// Constructor
public Deck() {
  for(String number : numberList) {
    for(String suit : suitList) {
     cardDeck.add(new Card(number, suit));
    }
  }
}

@Override
public Card get(int i) { return cardDeck.get(i); }

@Override
public int size() { return cardDeck.size(); }

}

使用AbstractList,您可以在Deck内的卡片列表中获得一种查看。然后,您可以像普通列表一样使用套牌(它是只读的,没有add(Card)set(int, Card)操作!)。

例如,在您的主要课程中:

package cardgame;

public class CardGame {

  public static void main(String[] args) {
      Deck newDeck = new Deck();
      // Pick the card at index 1
      System.out.println(newDeck.get(1));
      // Iterate over all cards in the Deck and print them.
      // This is basically sugar from AbstractList.
      for(Card card : newDeck) {
        System.out.println(card.toString());
      }
  }

}

答案 1 :(得分:0)

您必须指定arraylist存储的类型。

更改此内容:private ArrayList cardDeck = new ArrayList();

private List<Card> cardDeck = new ArrayList<>();


此外,这不是构造函数,在创建实例时不会被执行:

public ArrayList Deck()

您必须将其更改为:

public Deck()

答案 2 :(得分:0)

各种评论:

  • Deck类中的Deck方法是一个构造函数,它不应该返回任何东西。
  • 你的套牌中没有get(int)方法,添加一个,或者添加一个返回cardDeck的getDeck()。
  • 这不是错误,只是建议使代码更容易阅读:在循环中用numberList.length和suitList.length替换4和13,或者甚至更好地将代码更改为:< p>

    for(String number : numberList) 
      for(String suit : suitList) {
         cardDeck.add(new Card(number, suit));