Java函数在列表之间传输数据?
我正在尝试制作扑克游戏,并希望在我的 ArrayList“牌组”之间传输数据并拥有另一个列表“手牌”。我已对我的列表进行了硬编码,以包含标准牌组中的所有 52 张牌,并希望通过引用该牌组中牌的位置,将列表牌组中的第一张牌弹出到列表手牌中。由于洗牌,我无法引用卡牌的名称来转移它,只需要卡牌的位置即可转移到手上。有谁知道执行此操作的函数、硬编码的方法甚至在哪里查找。如果需要的话我可以澄清任何事情。
I am trying to make a poker game and would like to transfer data between my ArrayList "deck" and have another list "hand". I have hard-coded my list to include all 52 cards in a standard deck and want to pop the first card off of the list deck to the list hand by referencing the location of the card in the deck. Due to shuffling the deck I can't reference the name of the card to transfer it and just need the location of the card to transfer to the hand. Does anyone know a function to do this, a way to hard-code this or even where to look. I can clarify anything if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
hand
和deck
都是ArrayList
类型,那么它很简单:请注意,
ArrayList
不是这是一个特别好的类,因为它不是为有效删除列表末尾以外的条目而设计的。If both
hand
anddeck
are of typeArrayList<Card>
then it's simply:Note that
ArrayList
isn't a particularly good class for this, as it's not designed for efficient removal of entries other than at the end of the list.如果将一副牌存储在
ArrayDeque
。通过这样做,您可以调用Collections.shuffle
来洗牌,当您处理时,只需pop
从牌堆中取出下一张牌,呃,Deque.
You might find it easier if you store your deck of cards in an
ArrayDeque
. By doing that you could callCollections.shuffle
to shuffle the deck and as you're dealing justpop
the next card off the deck, er,Deque
.您可以使用 remove(int index )。它返回索引处的对象并将其从列表中删除。
You could use remove(int index). It returns the object at index and removes it from the list.
您应该使用 Stack牌组,因为这就是牌组的运作方式,它确实从集合中“弹出”第一个对象。 (您还可以使用通用队列接口来获得更多信息现代的、类型安全的方法,并且您的实现仍然可以是列表集合。)
以下是有关 打乱堆栈。洗牌后的堆栈的行为就像一副纸牌一样。
You should use a Stack for the deck since that is how card deck operates and it does indeed "pop" the first object out of the collection. (You could also use the generic Queue interface for a more modern, type-safe approach and you implementation could still be a list collection.)
Here's some info on shuffling a Stack. A shuffled Stack then behaves exactly like a card deck.