0

我正在为我的 CS 课程中的二十一点游戏制作一袋纸牌。这个特定的项目需要我创建一个袋子来容纳我的 52 张卡片。请记住,我试图确保每张牌都有 4 种类型,包括 Q、K、J 和 A。我的 main 中不断出现错误:线程“main”中的异常 java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 [Ljava.lang.Integer; 在 Main.main(Main.java:14)

如果有人可以帮助我让这个包正常运行,将不胜感激。

这是我的代码:

public class Bag<T> 
{
    T[] cards;
    private final int DEFAULT_CAPACITY = 52;
    private int numberOfEntries;

    public Bag()
    {
        this.cards = (T[]) new Object[DEFAULT_CAPACITY];
        numberOfEntries = DEFAULT_CAPACITY;
    }

    public int getCurrentSize()
    {
        return numberOfEntries;
    }

    public boolean isFull()
    {
        return numberOfEntries == DEFAULT_CAPACITY;
    }

    public boolean isEmpty()
    {
        return numberOfEntries == 0;
    }

    public boolean add(T newItem)
    {
        boolean result = true;
        if(isFull())
        {
            result = false;
        }

        else
        {
            cards[numberOfEntries] = newItem;
            numberOfEntries++;
        }

        return result;
    }

    public boolean remove()
    {
        boolean result = true;
        if(numberOfEntries > 0)
        {
            numberOfEntries--;
        }
        else
            result = false;

        return result;
    }

    public void clear()
    {
        numberOfEntries = 0;
    }

    public int getNumOf(T anItem)
    {
        int count = 0;

        for(int i = 0; i < cards.length; i++)
        {
            if(anItem.equals(cards[i]))
            {
                count++;
            }
        }

        return count;
    }

    public boolean contains(T anItem)
    {
        boolean found = false;

        for (int i = 0; !found && (i < numberOfEntries); i++)
        {
            if(anItem.equals(cards[i]))
            {
                found = true;
            }
        }

        return found;
    }

    public T Grab()
    {
        int random = (int)(Math.random() * DEFAULT_CAPACITY);
        if(!isEmpty())
        {
            cards[random] = null;
            numberOfEntries--;
            return cards[random];
        }

        else
            return null;
    }

    public int getFrequencyOf(T anItem)
    {
        int counter = 0;

        for(int i = 0; i < numberOfEntries; i++)
        {
            if(anItem.equals(cards[i]))
            {
                counter++;
            }
        }

        return counter;
    }

}

public class Main {

    public static void main(String[] args)
    {
        //Accesses the Bag class
        Bag<Integer> bag = new Bag<Integer>();

        //Sets up 52 cards (13*4). 4 of each type
        for (int i = 1; i <= 13; i++) 
        {

            for (int j = 1; j <= 4; j++) {
                bag.cards[i*j] = i;
                //if the card is an ace and not equal to 1
                if(i == 1)
                    bag.cards[i*j] = 11;
                //handles the king, queen, and jack cards
                else if (i==11||i==12||i==13)
                    bag.cards[i*j] = 10;    
            }

            bag.add(1);
        }
    }
}
4

2 回答 2

1

您不能将 Object 数组转换为 Integer 数组,因为它不是,它是 Object 数组。这就是您的 (T[]) 演员试图做的事情:

this.cards = (T[]) new Object[DEFAULT_CAPACITY];

您也不能实例化 T 类型的数组,除非您显式传入类类型 - 请参阅What's the reason I can't create generic array types in Java?. 可能的解决方案:

  • 使用 Object[] 存储您的数据,即私有 Object[] 卡片;
  • 使用参数化集合类型,如List<T>, i.e. private List<T> cards
于 2015-02-10T21:55:16.290 回答
1

不要提供对T[] cards变量的访问。制作它private并像这样创建一个set方法Bag

public void set(int index, T item) {
  // assume !full AND 0 <= index < cards.length 
  this.cards[index] = item;
}

然后,而不是这样做:

bag.cards[i*j] = 10;

然后你做:

bag.set(i*j, 10);    

你得到一个类转换异常的事实是因为类型擦除:你T[]存在于编译时。编译后,它就会变成一个Object[]. 这就是为什么您的直接访问cards[0] = 123会引发此异常(整数123不能放在 a 中Object[])。

我建议的set(int index, T value)工作,因为在编译后,它将变成set(int index, Object value),因此:没有类转换异常。

编辑

您可以测试以下快速演示:

class Bag<T> {

  private T[] cards;

  public Bag() {
    this.cards = (T[]) new Object[10];
  }

  public void set(int index, T value) {
    this.cards[index] = value;
  }

  @Override
  public String toString() {
    return "Bag{cards=" + java.util.Arrays.toString(cards) + "}";
  }

  public static void main(String[] args) {
    Bag<Integer> bag = new Bag<Integer>();
    bag.set(0, 10);
    bag.set(1, 20);
    bag.set(2, 30);
    System.out.println(bag);
  }
}

Ideone上,将打印:

Bag{cards=[10, 20, 30, null, null, null, null, null, null, null]} 

您也可以cards像这样简单地从变量中删除泛型:

class Bag<T> {

  private Object[] cards;

  public Bag() {
    this.cards = new Object[10];
  }

  public void set(int index, T value) {
    this.cards[index] = value;
  }
}

为了获得灵感,您可以随时查看与您自己类似的核心 Java 类的来源。在这种情况下,那就是java.util.ArrayList: http: //www.docjar.com/html/api/java/util/ArrayList.java.html

于 2015-02-10T21:50:24.363 回答