将对象添加到Java中的一个ArrayList [英] Adding objects to an arraylist in java
问题描述
我试图写在Java中的solitiare游戏。我试图卡对象添加到我的ArrayList,但MakeDeck功能不能正常工作。
公共类PileOfCards {
公众的ArrayList<卡>一堆=新的ArrayList<卡>();
卡的速度;公共PileOfCards(){
一堆=新的ArrayList<卡>();}公共无效MakeDeck(){ 对(INT I = 0; I&; 13;我++){
速度=新卡(I + 1面朝下,俱乐部);
pile.add(速度); }
为(中间体J = 0; J&; 13; J ++){
速度=新牌(J + 1面朝下,钻石);
pile.add(速度);
}
为(中间体K = 0; K&; 13; k ++){
速度=新卡(K + 1面朝下,心);
pile.add(速度);
}
为(int类型l = 0; L&; 13:L ++){
速度=新卡(L + 1,面朝下,黑桃);
pile.add(速度);
}
}
当我试图打印打印出数值0,空,空,52倍。有什么问题?它看起来像我不能达到到ArrayList,但我不知道为什么。
编辑:
卡的构造函数:
市民卡(INT valueTemp,字符串suitTemp,字符串statusTemp){
valueTemp =价值;
suitTemp =套装;
statusTemp =状态;
}
打印功能:
公共无效printPile(){
的for(int i = 0; I< pile.size();我++){
System.out.print(pile.get(我).STATUS);
System.out.print();
System.out.print(pile.get㈠.suit);
System.out.print();
System.out.print(pile.get(我)。价值);
System.out.printf(\\ n);
}
这个问题是在你的卡
构造函数;该分配是走错了路周围。
您想
市民卡(INT valueTemp,字符串suitTemp,字符串statusTemp){
值= valueTemp;
套装= suitTemp;
状态= statusTemp;
}
周围的另一种方法不设置为等于参数字段,它设置等于字段的参数。具体来说,你以前有什么
市民卡(INT valueTemp,字符串suitTemp,字符串statusTemp){
valueTemp =价值;
suitTemp =套装;
statusTemp =状态;
}
设定 valueTemp
到值
, suitTemp
到套装
和 statusTemp
到状态
,当你真正想做其他方式在构造函数。
I am trying to write a solitiare game on java. I'm trying to add the card objects to my arraylist but the MakeDeck function is not working properly.
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. What is the problem? It looks like I can't reach to the ArrayList, but I don't know why.
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; 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.
这篇关于将对象添加到Java中的一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!