The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Trying to create an array of stacks that contain objects

I have tried so many things, I am now thorougly confussed. I am trying to make an array of Stacks that will contain objects called Card. I need 17 stacks of cards. The currentDeck.dealSingleCard() returns a Card object.

Here's my last stab that gives me the error message no suitable method found for add. I've tried with ArrayList, but that gives me an error message about generics. Surely there is an easy way to create a bunch of stacks withouth having to give each one an separate name..... Help :-)

Re: Trying to create an array of stacks that contain objects

Hi jadeclan and welcome to the forum.
Please see the announcements page for help on the use of code tags and other useful information related to the forum.

...that gives me the error message no suitable method found for add...
...that gives me an error message about generics...

When posting about error messages, a copy paste of the full error message is a great choice. That way any information important enough to make it to the error message also makes it to the people trying to help figure out the cause of the problem.

You mentioned trying different methods and different errors, please post your question again including the current version of the code and related error messages. Yes I see you listed this as your last stab I just think a new all-in-one post would be a nice place to start. I would suggest you set up the code the way you did the first time, the way the code felt natural to you, and go from there. If there is a different way to do something that may provide some advantage, someone will surely point you in that direction.

Re: Trying to create an array of stacks that contain objects

I have taken your advice and gone back to where I started, this is what I have:

publicclass Solitaire_Engine
{privateStack<Card> piles []=newStack<Card>[17];// will use 4 stacks for foundations, 6 stacks for hidden cards and 7 stacks// for the visible cards. Note no stack required for the deck or for the current// card that came from the deck.private Deck currentDeck;// current deck of cards being played/**
* Constructor for objects of class Solitaire_Engine
*/public Solitaire_Engine(Deck currentDeck){this.currentDeck= currentDeck;for(int i =0; i <17; i++){
piles[i].clear();}
setUpPiles();}publicvoid setUpPiles(){// Note at game start up, the hidden cards in each pile are dealt. ie, they do not get dealt at// the time the visible cards have been moved.// the array positions 0 - 6 are stacks used to store visible cards (not foundation cards)for(int i =0; i <7; i++){
piles[i]= piles[i].push(currentDeck.dealSingleCard());}

Re: Trying to create an array of stacks that contain objects

Please post the entire error message.

Likely though your problem is due to the limitations of Java generics including type erasure and that Java generics are not covariant. To read more on this, and in particular the covariance issue which is very relevant to your problem, please have a look here: Java theory and practice: Generics gotchas. Look in particular at the More covariance troubles section.

Re: Trying to create an array of stacks that contain objects

I read the theory and think I grasp what's been said, but I'm still left with the question: Do I need to create 17 stacks of different names rather than use an array/ arraylist/ list / other???? If I need 17, I certainly can do it, but what a pain in the posterior lol

Re: Trying to create an array of stacks that contain objects

and got an unexpected type in the body of the last for loop. (error message was: unexpected type required variable, found value)

I also tried adding the following line within the first for loop
piles.add( new Stack<Card>() );
in case it was an initialization problem --- that didn't help.

publicclass Solitaire_Engine
{private List<Stack<Card>> piles;// will use 4 stacks for foundations, 6 stacks for hidden cards and 7 stacks// for the visible cards. Note no stack required for the deck or for the current// card that came from the deck.private Deck currentDeck;// current deck of cards being played/**
* Constructor for objects of class Solitaire_Engine
*/public Solitaire_Engine(Deck currentDeck){this.currentDeck= currentDeck;
piles =new ArrayList<Stack<Card>>();for(int i =0; i <17; i++){
piles.get(i).clear();}
setUpPiles();}publicvoid setUpPiles(){// Note at game start up, the hidden cards in each pile are dealt. ie, they do not get dealt at// the time the visible cards have been moved.// the array positions 0 - 6 are stacks used to store visible cards (not foundation cards)for(int i =0; i <7; i++){
piles.get(i)= piles.get(i).push(currentDeck.dealSingleCard());}

Re: Trying to create an array of stacks that contain objects

The error occurs on the line:
piles.get(i) = (Stack) piles.get(i).push(currentDeck.dealSingleCard());
which is second to last in the code below.

You are right I was clearing something that didn't exist, but it didn't make any difference....... it's fixed in the code below in any event.

publicclass Solitaire_Engine
{private List<Stack<Card>> piles;// will use 4 stacks for foundations, 6 stacks for hidden cards and 7 stacks// for the visible cards. Note no stack required for the deck or for the current// card that came from the deck.private Deck currentDeck;// current deck of cards being played/**
* Constructor for objects of class Solitaire_Engine
*/public Solitaire_Engine(Deck currentDeck){this.currentDeck= currentDeck;for(int i =0; i <17; i++){
piles.add(new Stack<Card>());
piles.get(i).clear();}
setUpPiles();}/*
* setUpPiles() method to set up the initial stacks for the playing of the solitaire game
*/publicvoid setUpPiles(){// Note at game start up, the hidden cards in each pile are dealt. ie, they do not get dealt at// the time the visible cards have been moved.// the array positions 0 - 6 are stacks used to store visible cards (not foundation cards)for(int i =0; i <7; i++){
piles.get(i)=(Stack) piles.get(i).push(currentDeck.dealSingleCard());}

Re: Trying to create an array of stacks that contain objects

You seem to be calling get on an ArrayList and then trying to set that equal to a stack. That's not how ArrayLists or the get() method works. Why not instead simply call add(...) on the ArrayList if you want to add a Stack to it?

In other words, myArrayList.get(...) should never be by itself on the left side of an assignment statement. Only a variable can go there.

Re: Trying to create an array of stacks that contain objects

Fishing for an aha moment (for me) in your last response, I tried changing the offending line to:
piles.get(i).push(currentDeck.dealSingleCard());

and

tada no error

you are a genius.... thanks for the help..... I'm not done yet, but the ebbs and flows that I went through
to get to this point have clarified a number of things, which will make this assignment easier.