Results 1 to 3 of 3
  1. #1
    tepichi09 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Storing objects into an arraylist in a different class?

    Hello there, I have just recently strated using Java and I am trying to find out how to get objects created in one class to be stored into an arraylist in a different class.
    I have four classes two of which are card and deck. I am trying to get card objects stored into the deck class arraylist but I'm not sure how. If anyone can explain and give an example I would really appreciate it.

  2. #2
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Storing objects into an arraylist in a different class?

    You will need to use the import java.util.ArrayList; this is placed right at the top of class, before anything else.

    Then your instance variable will be something like this:

    public class Deck
    {
    private ArrayList<Card> DeckOfCards;

    Then to add new cards to your deck you will create a method for this, using the new function in java.

    public void addCard(Card newCard)
    {
    DeckOfCards.add(newCard);
    }

    So what you will do is create you cards objects, then create you deck object. Once the deck object is created, right click on it and select the method newCard. If you double click on any of the card objects this will add the card to your deck. You can do this for each card and the array will be built up.

    Hopefully this make sense.

  3. #3
    tepichi09 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Storing objects into an arraylist in a different class?

    I'm still getting the same problem as before. I'm using the jGRASP IDE and its saying cannot find symbol. Here is my two classes with the addCard method you mentioned.

    * Card
    */
    import java.util.ArrayList;
    public class Card
    {
    private String suite;
    private int value;
    private Card aCard;
    ArrayList<String> suits;

    public Card(int val, String suit)
    {
    suits = new ArrayList<String>();
    suits.add("Hearts");
    suits.add("Diamonds");
    suits.add("Spades");
    suits.add("Clubs");
    }

    public void setCardValues()
    {
    for(int n = 0;n <= 3;n++)
    {
    for(int value = 1; value < 14; value++)
    {
    aCard = new Card(value, suits.get(n));
    cardDeck.add(aCard);
    }
    }
    }




    }


    * Deck
    */
    import java.util.ArrayList;
    import java.util.Random;

    public class Deck
    {
    private static Random rand = new Random();
    ArrayList<Card> cardDeck;


    public Deck()
    {
    cardDeck = new ArrayList<Card>();
    }

    public void addCard(Card aCard)
    {
    cardDeck.add(aCard);
    }




    }

Similar Threads

  1. Replies: 6
    Last Post: 11-02-2011, 03:38 AM
  2. Storing objects in an array list?
    By Moedig in forum New To Java
    Replies: 3
    Last Post: 10-27-2011, 06:39 PM
  3. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 06:12 AM
  4. Replies: 2
    Last Post: 10-28-2010, 02:33 PM
  5. Storing objects directly with db4o
    By german in forum JDBC
    Replies: 0
    Last Post: 05-12-2009, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •