简体   繁体   English

如何修改其他班级的arraylist? Java的

[英]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

  1. Java convention is to lowercase methods (eg Draw ). Java约定是小写方法(例如Draw )。
  2. Initializing variables inside the constructor is often clearer. 初始化构造函数中的变量往往是更清晰。
  3. Declaring 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.

相关问题 如何从不同的 class 修改 JTextfield 的文本? (爪哇) - How do I modify the text of a JTextfield from a different class? (Java) Java:我如何从不同的 class 调用数组,我要修改它们吗? - Java: How do I call the array from different class, and I do I modify them? Java-如何修改一个类中的静态ArrayList和另一个类中的ActionListener? - Java - How do I modify static ArrayList in one class with an ActionListener in another class? 如何从 Java 中的不同 class 访问 arraylist? - How can I access an an arraylist from a different class in Java? 如何在Java中将数组列表从一个类复制到另一个类? - How do I copy an arraylist from one class to another in Java? 如何在单独的活动中从一个活动修改ArrayList - How do i modify an ArrayList from one Activity in a separate Activity 你如何从一个不同的class中调用一个变量,并把它添加到Java中的一个ArrayList的末尾? - How do you call a variable from a different class and add it to the end of an ArrayList in Java? 如何将变量从Java类加载到其他类 - How do I load an variable from a java class to a different class 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 如何创建一些对象来存储在一个arrayList中,这是一个子类的属性,来自Java中的父类? - How do I create some objects to store in an arrayList, which is a attribute of sub class, from a parent class in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM