[英]How do I modify an arraylist from a different class? Java
I'm writing a poker game, and have all of the cards stored in an arraylist created in the class Deck: 我正在写一个扑克游戏,并将所有卡存储在Deck类中创建的arraylist中:
public class Deck {
ArrayList<Card> deck = new ArrayList<Card>();
Deck(){ //Build the deck in order.
for(int suit = 1; suit <= 4; suit++){
for(int rank = 1; rank <= 13; rank++){
Card card = new Card(rank, suit);
deck.add(card);
}
}
}
. 。 . 。 . 。
I want to have another class--Hand--draw the first five elements from the arraylist and put them into a new arraylist called myHand: 我想要另一个类 - 手工绘制arraylist中的前五个元素并将它们放入一个名为myHand的新arraylist中:
public class Hand {
ArrayList<Card> myHand = new ArrayList<Card>();
Deck deckObject = new Deck();
public void Draw(){ //Draws the top 5 cards of the deck and puts them in your hand ArrayList.
for(int i = 0; i <= 4; i++){
myHand.add(deckObject.deck.get(0));
deckObject.deck.remove(0);
}
}
. 。 . 。 . 。 So far so good. 到现在为止还挺好。 When I display the hand ArrayList from the main class I get the first five cards in the deck. 当我从主类中显示手ArrayList时,我得到了牌组中的前五张牌。 However, when I display the deck from the Deck class (after invoking the Draw() method) all 52 cards are still there. 但是,当我从Deck类显示牌组时(在调用Draw()方法之后),所有52张牌仍在那里。
If I create a getDeck() method in the Hand class and call it, the first five cards are removed as expected... 如果我在Hand类中创建一个getDeck()方法并调用它,前五张卡将按预期删除...
So it seems like I have one-way communication between these two classes; 所以我似乎在这两个类之间进行单向通信; when I modify the ArrayList from the Hand class, the Deck class doesn't know about it, and it seems that each class is keeping a separate version of the ArrayList. 当我从Hand类修改ArrayList时,Deck类不知道它,并且似乎每个类都保留了ArrayList的单独版本。 What's going on here? 这里发生了什么?
Each Hand
has its own Deck
. 每只Hand
都有自己的Deck
。 You want to share one Deck
among many hands. 你想在很多人手中分享一个Deck
。
I think you want something like this: 我想你想要这样的东西:
public class Hand {
Deck deck;
ArrayList<Card> myHand = new ArrayList<Card>();
Hand(Deck deck) {
this.deck = deck;
}
public void removeCard(Card card) {
deckObject.deck.remove(card);
}
public void Draw() {
for(int i = 0; i <= 4; i++) {
myHand.add(deckObject.deck.get(0));
deckObject.deck.remove(0);
}
}
}
and then 接着
Deck deck = new Deck();
Hand hand1 = new Hand(deck);
Hand hand2 = new Hand(deck);
hand1.Draw();
hand2.Draw();
FYI FYI
Draw
). Java约定是小写方法(例如Draw
)。 List<Card> myHand
is usually preferred since it has a higher level of abstraction. 声明List<Card> myHand
通常是首选,因为它具有更高的抽象级别。 I would try this in your Hand
method Draw()
which should really be named draw()
. 我会在你的Hand
方法Draw()
尝试这个,它应该真正命名为draw()
。
if (deckObject.deck.size() > 0) { // Are there cards in the deck?
myHand.add(deckObject.deck.remove(0)); // add the card removed from
// the deck to the Hand.
} else {
break; // No cards.
}
// deckObject.deck.remove(0); // Already taken care of.
Generally speaking, you don't want anybody else messing with the internal state of you objects. 一般来说,你不希望别人搞乱你对象的内部状态。 Instead, you want to provide methods which allow other objects to interact with it, for example... 相反,您希望提供允许其他对象与其交互的方法,例如......
public class Deck {
private ArrayList<Card> deck = new ArrayList<Card>();
Deck(){ //Build the deck in order.
for(int suit = 1; suit <= 4; suit++){
for(int rank = 1; rank <= 13; rank++){
Card card = new Card(rank, suit);
deck.add(card);
}
}
}
public Card[] draw(int count) {
Card[] cards = new Card[count];
if (count < deck.size()) {
for(int i = 0; i < count; i++){
cards[i] = deckObject.deck.remove(0);
}
} else {
throw new IllegalStateException("Can not with draw " + (count + 1) + " from deck that only has " + deck.size() + " cards");
}
return cards;
}
In this way, you protect the internal state of the Cards
while providing the ability of other objects to interact with your Deck
这样,您可以保护Cards
的内部状态,同时提供其他对象与您的Deck
交互的能力
This also deals with the management and logic within a single place, so that it always the same... 这也涉及到一个地方的管理和逻辑,所以它总是一样的......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.