简体   繁体   English

在Java中将对象添加到arraylist

[英]Adding objects to an arraylist in java

I am trying to write a solitiare game on java. 我试图在Java上编写一个纸牌游戏。 I'm trying to add the card objects to my arraylist but the MakeDeck function is not working properly. 我正在尝试将卡对象添加到我的arraylist中,但是MakeDeck函数无法正常工作。

public class PileOfCards {
public ArrayList<Card> pile = new ArrayList<Card>();
Card tempo;

public PileOfCards() {
  pile = new ArrayList<Card>();

}

public void MakeDeck() {

    for (int i=0;i<13;i++){
        tempo = new Card(i+1, "FaceDown", "Clubs");
        pile.add(tempo);

    }
    for (int j=0;j<13;j++){
        tempo = new Card(j+1, "FaceDown", "Diamonds");
        pile.add(tempo);
}
    for (int k=0;k<13;k++){
        tempo = new Card(k+1, "FaceDown", "Hearts");
        pile.add(tempo);
    }
    for (int l=0;l<13;l++){
        tempo = new Card(l+1, "FaceDown", "Spades");
        pile.add(tempo);
    }
} 

When I'm trying to print the values it prints "0, null, null" , 52 times. 当我尝试打印值时,它将打印“ 0,null,null” 52次。 What is the problem? 问题是什么? It looks like I can't reach to the ArrayList, but I don't know why. 看来我无法到达ArrayList,但我不知道为什么。

Edited: 编辑:

Card constructor: 卡构造函数:

   public Card(int valueTemp, String suitTemp, String statusTemp) {
    valueTemp = value;
    suitTemp = suit;
    statusTemp = status;
    }

Print function: 打印功能:

 public void printPile(){
     for(int i=0;i<pile.size();i++){
     System.out.print(pile.get(i).status);
     System.out.print(" ");
     System.out.print(pile.get(i).suit);
     System.out.print(" ");
     System.out.print(pile.get(i).value);
     System.out.printf("\n");
 }

The issue is in your Card constructor; 问题出在您的Card构造函数中; the assignments are the wrong way around. 分配是错误的方法。

You want 你要

public Card(int valueTemp, String suitTemp, String statusTemp) {
    value = valueTemp;
    suit = suitTemp;
    status = statusTemp;
}

The other way around doesn't set the fields equal to the parameters, it sets the parameters equal to the fields. 另一种方法是不将字段设置为等于参数,而是将参数设置为等于字段。 Specifically, what you had before 具体来说,你以前有过

public Card(int valueTemp, String suitTemp, String statusTemp) {
   valueTemp = value;
   suitTemp = suit;
   statusTemp = status;
}

sets valueTemp to value , suitTemp to suit , and statusTemp to status , when you actually wanted to do the other way around in your constructor. 当您实际上想在构造函数中进行其他操作时,将valueTemp设置为value ,将suitTempsuit ,并将statusTempstatus

public Card(int valueTemp, String suitTemp, String statusTemp) {
  valueTemp = value;
  suitTemp = suit;
  statusTemp = status;
}

All those assignments are back to front. 所有这些任务都排在最前面。 You are assigning to the parameters instead of from them. 要指定参数,而不是他们。 It is customary to write it like this: 通常习惯这样写:

public Card(int value, String suit, String status) {
  this.value = value;
  this.suit = suit;
  this.status = status;
}

The assignments are backwards. 分配是向后的。

lValue = rValue ; lValue = rValue

lValue is the thing you want to set while rValue is the data you want it to become. lValue是要设置的内容,而rValue是要使其成为数据的数据。

When you did: 当您这样做时:

public Card(int valueTemp, String suitTemp, String statusTemp) {
    valueTemp = value;
    suitTemp = suit;
    statusTemp = status;
}

You're changing the parameters valueTemp , suitTemp , and statusTemp which you passed in as parameters to whatever value , suit , and status are. 您正在将作为参数valueTemp的参数valueTempsuitTempstatusTempvaluesuitstatus

To fix this, flip around the values you're assigning like so: 要解决此问题,请像这样翻转周围分配的值:

public Card(int valueTemp, String suitTemp, String statusTemp) {
    value = valueTemp;
    suit = suitTemp;
    status = statusTemp;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM