Java Can';t在main中使用ArrayList方法

Java Can';t在main中使用ArrayList方法,java,class,arraylist,undefined,Java,Class,Arraylist,Undefined,我想写一个模拟纸牌游戏。我写了我的类和函数。我将尝试在ArrayList中保存我的卡对象。但是大体上我不能使用ArrayList的方法。我想我在Representation类中创建了ArrayList类型的PileOfCards对象。因为在PileOfCards类中,我在构造函数中创建了一个ArrayList。所以我想让每次我们从PileOfCards类创建一个对象时,它的类型都是ArrayList。我的意思是我想在我的PileOfCards对象上使用ArrayList方法。但是在我的主要代码中

我想写一个模拟纸牌游戏。我写了我的类和函数。我将尝试在ArrayList中保存我的卡对象。但是大体上我不能使用ArrayList的方法。我想我在Representation类中创建了ArrayList类型的PileOfCards对象。因为在PileOfCards类中,我在构造函数中创建了一个ArrayList。所以我想让每次我们从PileOfCards类创建一个对象时,它的类型都是ArrayList。我的意思是我想在我的PileOfCards对象上使用ArrayList方法。但是在我的主要代码中,我得到了一个错误“PileOfCards类型的方法size()未定义”和get(3)。我相信PileOfCards对象是ArrayList类型的。为什么我会犯这样的错误。任何帮助都将不胜感激

// MY CARD CLASS
public class Card 
{

/* I assigned variables to numbers because I can use them later easily.
 * And they can not be changed
 */
public final static int red     = 0;
public final static int black    = 1;

public final static int heart = 0;
public final static int spade = 1;
public final static int diamond = 2;
public final static int club = 3;

public final static int ace     = 0;
public final static int jack    = 10;
public final static int queen    = 11;
public final static int king    = 12;

public static String values[] = {"A" , "2" , "3" , "4" , "5" , "6" , "7",
                                "8" , "9" , "10" , "J" , "Q" , "K"};   

private int value;
private int suit;
boolean faceUp;

// Create card object with parameters.
Card(int value_in, int suit_in, boolean face_type)
{

    value = value_in;
    suit = suit_in;
    faceUp = face_type;
}

// Make the card face up
public void faceUp()
{
    faceUp = true;
}

// Make the card face down
public void faceDown()
{
    faceUp = false;
}

// return card is face up or face down
public final boolean cardStatus()
{
    return faceUp;
}

// return the value of the card
public final int cardValue()
{
    return value;
}

// return the suit of the card
public final int cardSuit()
{
    return suit;
}

// return the color of the card
public final int color()
{
    if(cardSuit()==heart || cardSuit()==diamond)
    {
        return red;
    }
    else
    {
        return black;
    }
}

public String toString() {
    return ("Card Suit: "+this.cardSuit()+
            " Card Value: "+ this.cardValue() +
            " Card Status: "+ this.cardStatus() +
            " Color : " + this.color());
   }
}
//--------------------------------------------------------------------------------

// MY PILEOFCARDS CLASS
import java.util.*;

public class PileOfCards 
{
/* I created pile as ArrayList because the elements of the list will change.
 * So using ArrayList is more useful and easier.
 */
public ArrayList<Card> pile;

//create an empty pile
PileOfCards()
{

    // Create pile with card objects
    this.pile = new ArrayList<Card>();
}

//create a deck with 52 cards
public void makeDeck()
{

    for (int a=0; a<=3; a++)
    {
        for (int b=0; b<=12; b++)
        {
            pile.add(new Card(a, b, false)); 
        }
    }

}

// shuffling the cards
public void shuffle()
{

    Collections.shuffle(this.pile);
}

// get the top card and remove that. I think the first element of the list as top card
public Card takeTopCard()
{

    if(pile.isEmpty()) 
    {
        return null; 
    }
    else 
    {    
        Card top = pile.get(0);
        pile.remove(0);
        return top;
    }
}

// add a specific card to the pile
public void addCardToPile(Card c)
{

    pile.add(c);
}

// determines the pile can take card
public boolean canTakeSuit (Card aCard)  
{   if (pile.isEmpty())
        return aCard.cardValue() == 0;
    Card topCard = pile.get(0);
    return (aCard.cardSuit() == topCard.cardSuit()) &&
        (aCard.cardValue() == 1 + topCard.cardValue());
}


public boolean canTakeValue (Card aCard) 
{   if (pile.isEmpty())
        return aCard.cardValue() == 12;
    Card topCard = pile.get(0);
    return (aCard.color() != topCard.color()) &&
           (aCard.cardValue() == topCard.cardValue() - 1);
}

public void selectSuit () 
{   if (pile.isEmpty())
        return;

    // if face down flip the card
    Card topCard = pile.get(0);
    if ( topCard.cardStatus() != true) 
    {   
        topCard.faceUp();
    }

    pile.remove(0);

    if(canTakeSuit(topCard))
    {
        pile.add(topCard);
    }
    else
    {
    addCardToPile(topCard);
    }
}

public void selectValue () 
{   if (pile.isEmpty())
        return;

    // if face down flip the card
    Card topCard = pile.get(0);
    if ( topCard.cardStatus() != true) 
    {   
        topCard.faceUp();
    }

    pile.remove(0);

    if(canTakeValue(topCard))
    {
        pile.add(topCard);
    }
    else
    {
    addCardToPile(topCard);
    }
}
}
    //--------------------------------------------------------------------------
// MY REPRESENTATION CLASS
public class Representation
{

// create the main pile. we will deal the cards from here
static PileOfCards mainPile = new PileOfCards();

// create a pile for opening cards. This pile can hold only one card.
static PileOfCards oneCardPile = new PileOfCards();

// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile1 = new PileOfCards();

// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile2 = new PileOfCards();

// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile3 = new PileOfCards();

// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile4 = new PileOfCards();

// create the pile that holds one card at the beginning - max. 13 cards
static PileOfCards firstPile = new PileOfCards();

// create the pile that holds 2 cards at the beginning - max. 13 cards
static PileOfCards secondPile = new PileOfCards();

// create the pile that holds 3 cards at the beginning - max. 13 cards
static PileOfCards thirdPile = new PileOfCards();

// create the pile that holds 4 cards at the beginning - max. 13 cards
static PileOfCards fourthPile = new PileOfCards();

// create the pile that holds 5 cards at the beginning - max. 13 cards
static PileOfCards fifthPile = new PileOfCards();

// create the pile that holds 6 cards at the beginning - max. 13 cards
static PileOfCards sixthPile = new PileOfCards();

// create the pile that holds 7 cards at the beginning - max. 13 cards
static PileOfCards seventhPile = new PileOfCards();
}
//-------------------------------------------------------------------------
// MY DEMO CLASS
import java.util.*;

public class Demo
{

public static void main(String[] args)
{

    Representation.mainPile.makeDeck();
    for(int i=0; i<Representation.mainPile.size(); i++)
    System.out.println(Representation.mainPile.get(i));
}

}
//我的卡片类
公务舱卡
{
/*我给数字分配了变量,因为我以后可以很容易地使用它们。
*它们是无法改变的
*/
公共最终静态整数红色=0;
公共最终静态整数黑色=1;
公共最终静态整数=0;
公共最终静态整数spade=1;
公共最终静态整数菱形=2;
公开决赛静态积分俱乐部=3;
公共最终静态int ace=0;
公共最终静态int插孔=10;
公共最终静态整数=11;
公共最终静态int king=12;
公共静态字符串值[]={“A”、“2”、“3”、“4”、“5”、“6”、“7”,
“8”、“9”、“10”、“J”、“Q”、“K”};
私有int值;
私人诉讼;
布尔面朝上;
//使用参数创建卡片对象。
卡片(整型值、整型套装、布尔型面)
{
值=中的值;
西服=西服;
faceUp=face_类型;
}
//把卡片正面朝上
公众虚空面对面()
{
faceUp=true;
}
//把卡片正面朝下
公共void faceDown()
{
faceUp=false;
}
//退货卡正面朝上或正面朝下
公共最终布尔cardStatus()
{
面朝上返回;
}
//返回卡的值
公共最终整数cardValue()
{
返回值;
}
//把卡片的套装退回来
公共最终int cardSuit()
{
反诉;
}
//返回卡片的颜色
公共最终整型颜色()
{
if(cardSuit()==心形| | cardSuit()==菱形)
{
返回红色;
}
其他的
{
返回黑色;
}
}
公共字符串toString(){
return(“卡片套装:+this.cardSuit()+
“卡值:”+this.cardValue()+
“卡状态:”+此.cardStatus()+
颜色:“+this.Color());
}
}
//--------------------------------------------------------------------------------
//我的皮尔沃兹班
导入java.util.*;
公营皮票
{
/*我创建了pile作为ArrayList,因为列表的元素将发生变化。
*因此,使用ArrayList更有用、更简单。
*/
公共阵列列表堆;
//创建一个空堆
桩板()
{
//使用卡片对象创建一堆
this.pile=新的ArrayList();
}
//创建一个包含52张牌的牌组
公共void makeDeck()
{

对于(int a=0;a只需在类
PileOfCards
中添加一个方法大小,它将返回arrayList的大小:

public int size(){
   return pile.size();
} 
要获得卡:

public Card get(int index){
   return pile.get(index);
}
我不是将PileOfCards对象创建为arraylist吗


不,您没有,
PileOfCards
是一个类,它可以构造
PileOfCards
类型的对象,这些对象的属性是
arrayList
您的
PileOfCards
类本身不是
arrayList
,它只有该类型的属性


使用
Representation.mainPile.pile.size()
而不仅仅是
Representation.mainPile.size()

我知道,但是为什么我不能使用arraylist的方法?我以后在代码中也会使用很多arraylist的方法。我不是把PileOfCards对象创建为arraylist吗?你必须使用
扩展arraylist
实现列表