Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

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

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 :-)


    List<Stack<Card>> piles = new List<Stack<Card>>();

    for (int i = 0; i < 17; i++)
    {
    piles.add(i) = ( new Stack <Card>() );
    }

    for (int i = 0; i < 7; i++)
    {
    piles.get(i) = piles.get(i).push(currentDeck.dealSingleCard());
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

     public class Solitaire_Engine
    {
        private Stack <Card> piles [] = new Stack <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();
        }
     
        public void 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());
            }


    the code goes on from here, but i get the following error

    generic array creation on the following line:
    private Stack <Card> piles [] = new Stack <Card> [17];
    Last edited by jadeclan; October 6th, 2012 at 01:30 PM.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    My poor eyes hurt so much. Please edit your post and use the code tags mentioned in my previous post.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    Sorry about that. Used tags, you're right it is a lot easier to read.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.
    Last edited by curmudgeon; October 6th, 2012 at 01:53 PM.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    I did post the complete error message.... I will have a look at the theory stuff, to see if I can deciper it.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

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

    Quote Originally Posted by jadeclan View Post
    I did post the complete error message.... I will have a look at the theory stuff, to see if I can deciper it.
    Yes you did. Sorry about that.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

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

    There's nothing preventing you from creating an ArrayList<Stack<Card>> or a List<Stack<Card>>.

    For example, this will compile just fine:
    List<Stack<String>> myStrStackList = new ArrayList<Stack<String>>();

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    I actually tried that in my attempts (see below)

    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.

     
    public class 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();
        }
     
        public void 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());
            }
    Last edited by jadeclan; October 6th, 2012 at 02:35 PM.

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

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

    Where *exactly* was the error? Which line?

    You appear to be trying to call clear() on Stacks that don't yet exist. You do this 17 times on an *empty* List of Stacks.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    the error says
    unexpected type
    expected: variable found: value

    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.

    public class 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
         */
        public void 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());
            }

  14. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  15. #15
    Junior Member
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    Thanks for the patience
    James

  16. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

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

    You're welcome and best of luck!

  17. The Following User Says Thank You to curmudgeon For This Useful Post:

    jadeclan (October 6th, 2012)

Similar Threads

  1. Replies: 1
    Last Post: June 29th, 2012, 01:29 PM
  2. Read .txt file and create objects using data
    By sevans20 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 14th, 2012, 01:52 PM
  3. Question Dynamically create objects, set value and call
    By buntyindia in forum Java Theory & Questions
    Replies: 1
    Last Post: May 25th, 2011, 07:50 AM
  4. How to create a pointer type of effect for objects
    By zelalem in forum Object Oriented Programming
    Replies: 6
    Last Post: October 20th, 2010, 02:53 AM
  5. Create Objects on Demand?
    By Programmierer in forum Object Oriented Programming
    Replies: 5
    Last Post: July 7th, 2010, 01:13 PM