简体   繁体   English

ArrayList是空的,但为什么呢?

[英]ArrayList is empty but why?

I'm trying to add Cards to ArrayList deck, but it doesn't seem to work(most of the code is an example on oracle.com ). 我正在尝试将卡添加到ArrayList卡,但它似乎不起作用(大多数代码是oracle.com上的一个示例)。 I'm probably doing something really stupid, but I can't seem to find it.. this is the code: 我可能正在做一些非常愚蠢的事情,但我似乎无法找到它..这是代码:

public class Card {

    public enum Rank 
    {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
        TEN, JACK, QUEEN, KING, ACE
    }

    public enum Suit
    {
        HEARTS, DIAMONDS, SPADES, CLUBS
    }

    private final Rank rank;
    private final Suit suit;
    private static final List<Card> deck = new ArrayList<Card>();

    public Card(Rank rank, Suit suit)
    {
        this.suit = suit;
        this.rank = rank;
    }
    // initializes deck
    public void initDeck()
    {
        for (Suit suit : Suit.values())
        {
            for (Rank rank : Rank.values())
            {
                deck.add(new Card(rank, suit));
            }
        }
    }
    // returns a copy of the deck    
    public static ArrayList<Card> newDeck()
    {
        return new ArrayList<Card>(deck);
    }

    public Rank getRank()
    {
        return rank;
    }

    public Suit getSuit()
    {
        return suit;
    }

    public String toString()
    {
        return rank +" of "+ suit;
    }

    public static void main(String[] args)
    {
        System.out.println(deck.toString());
    }

}

Your problem is that you never actually invoke initDeck, so the deck remains empty, as it was when the static initializer ran: 你的问题是你从未真正调用initDeck,因此当静态初始化程序运行时,套牌仍为空:

private static final List<Card> deck = new ArrayList<Card>();

Other problems: 其他问题:

  1. initDeck() is an instance method, but deck is a static reference. initDeck()是一个实例方法,但是deck是一个静态引用。
  2. initDeck() is a method on Card. initDeck()是Card上的一种方法。
  3. deck is a static member of Card , but a card does not own or define a deck. deckCard的静态成员,但是卡不拥有或定义甲板。
  4. newDeck is nonsensical in the face of a static final deck. 面对静态的最终套牌, newDeck是荒谬的。
  5. Cards should not be delivering decks anyway. 卡无论如何都不应该送甲板。

In short, your design is messed up - you need to think more and harder about the entities and their inter-relationships. 简而言之,你的设计搞砸了 - 你需要更多地思考实体及其相互关系。

it is very suspicious that your deck is private static final and your init method public and non-static . 你的deckprivate static final ,你的init方法是publicnon-static ,这是非常可疑的。 It can be initialized several times and contain more cards then you would expect. 它可以初始化几次并包含更多你想要的卡片。 You may want to do the initialization in static block instead. 您可能希望在静态块中进行初始化。

I would replace the code as follows: 我会替换代码如下:

  1. make the deck public static final and unmodifiable (via Collections) 使甲板public static final和不可修改(通过集合)
  2. copy the body of your init method in static initializer like this: 在静态初始化程序中复制init方法的主体,如下所示:
 static { for (Suit suit : Suit.values()) { for (Rank rank : Rank.values()) { deck.add(new Card(rank, suit)); } } } 
  1. delete initDeck completely 完全删除initDeck

It's simple, in your main method, you're just toString() an empty ArrayList . 这很简单,在你的main方法中,你只是toString()一个空的ArrayList You haven't called initDeck() method in your main method at all. 您根本没有在main方法中调用initDeck()方法。

When you ran your program private static final List<Card> deck = new ArrayList<Card>(); 当你运行你的程序private static final List<Card> deck = new ArrayList<Card>(); , deck was assigned an empty ArrayList . ,deck被分配了一个空的ArrayList

It's because initDeck() isn't called anywhere. 这是因为initDeck()不会在任何地方被调用。 Try to replace public void initDeck() with static . 尝试用static替换public void initDeck()

The keyword final in you List<card> definition is for object that will have an starting value that cannot be changed. List<card>定义中的关键字final是针对具有无法更改的起始值的对象。

Check it here 在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的ArrayList为空? - Why is my ArrayList Empty? 如果ArrayList为空,为什么不显示RecyclerView没有标题? - Why shows the RecyclerView no header if the ArrayList is empty? 空数组列表 - Empty Arraylist 为什么插入空ArrayList比插入非空ArrayList需要更多的时间? - Why insertion into empty ArrayList takes more time than insertion into non-empty ArrayList? 为什么即使添加对象,我的arraylist仍然为空? - Why does my arraylist stay empty despite adding objects? 为什么即使 Firebase 数据快照包含子项,我的 ArrayList 也是空的 - why is my ArrayList empty even though firebase datasnapshot contains children 为什么 ArrayList 是用空元素数组创建的,而 HashSet 是用空表创建的? - Why are ArrayList created with empty elements array but HashSet with null table? 在堆栈的ArrayList中,如果堆栈为空,为什么索引不正确? - In an ArrayList of Stacks, why are the indexes incorrect if a Stack(s) are empty? 为什么添加新对象后我的ArrayList为空? - Why is my ArrayList empty after adding new objects? 为什么我在解决幂集/子集问题时得到空的 2d ArrayList,例如 [ [ ]、[ ]、[ ]、...],而不是在 2d ArrayList 中添加 ArrayList? - Why I am getting empty 2d ArrayList such as [ [ ], [ ], [ ], ... ] instead of adding ArrayList in 2d ArrayList while solving power set/subset problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM