Results 1 to 3 of 3
- 11-06-2011, 06:50 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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.
- 11-06-2011, 07:10 PM #2
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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.
- 11-06-2011, 07:19 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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
-
repeatedly instantiating new objects and storing into an array.
By doranm09 in forum New To JavaReplies: 6Last Post: 11-02-2011, 03:38 AM -
Storing objects in an array list?
By Moedig in forum New To JavaReplies: 3Last Post: 10-27-2011, 06:39 PM -
Storing objects
By paul1024 in forum New To JavaReplies: 5Last Post: 04-30-2011, 06:12 AM -
Problem in storing Socket class object in ArrayList array
By Anoop in forum New To JavaReplies: 2Last Post: 10-28-2010, 02:33 PM -
Storing objects directly with db4o
By german in forum JDBCReplies: 0Last Post: 05-12-2009, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks