Results 1 to 7 of 7
Thread: Creating a list with objects
- 03-25-2011, 02:15 AM #1
Member
- Join Date
- Feb 2011
- Location
- NY
- Posts
- 5
- Rep Power
- 0
Creating a list with objects
Java Code:class DeckOfCards{ private List cards; public DeckOfCards(){ Card ace_Spades = new Card("Spades","Ace",11); Card two_Spades = new Card("Spades","Two",2); Card three_Spades = new Card("Spades","three",3); Card four_Spades = new Card("Spades","four",4); Card five_Spades = new Card("Spades","five",5); Card six_Spades = new Card("Spades","six",6); Card seven_Spades = new Card("Spades","seven",7); Card eight_Spades = new Card("Spades","eight",8); Card nine_Spades = new Card("Spades","nine",9); Card ten_Spades = new Card("Spades","ten",10); Card jack_Spades = new Card("Spades","jack",10); Card queen_Spades = new Card("Spades","queen",10); Card king_Spades = new Card("Spades","king",10); } List cards = new List(); cards.add(ace_Spades); }
I am trying to add each object or "element" to a list. Using the java list class. The goal is to add each to a list, and then shuffle the list latter on in a method. I am making a card game..
How would I go about adding each element to a list? To build my
- 03-25-2011, 02:20 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The add method is how you add to a list, I suggest you have the constrctor make an instance of the list. Then loop 52 times adding a new card each time. It may be helpful to have a card constructor which takes a single int as an argument and assigns the proper rank and suit based on the number(this can be done with % and /)
- 03-25-2011, 02:31 AM #3
Member
- Join Date
- Feb 2011
- Location
- NY
- Posts
- 5
- Rep Power
- 0
That is my card class.. Can you give me an example of the add method? I am not familiar with the List classclass Card{
private String suite;
private String type;
private int value;
public Card(String cardSuite, String cardType, int cardValue){
cardSuite = suite;
cardType = type;
cardValue = value;
}
}
- 03-25-2011, 03:01 AM #4
Member
- Join Date
- Feb 2011
- Location
- NY
- Posts
- 5
- Rep Power
- 0
I'm basically trying to add each card that has been instantiated into an List. I guess the correct term would be to add those to an Array Element List..
I tried
ArrayList cardList = new ArrayList();
cardList.add(ace_Spades);
not working
DeckOfCards.java:67: <identifier> expected
cardList.add(ace_Spades);Last edited by thine_iniquity; 03-25-2011 at 03:02 AM. Reason: oop
- 03-25-2011, 03:14 AM #5
Executable code, such as cardList.add(...) has to be inside a method, constructor or initializer block. It can't be executed in the class body, outside of any of the above.
db
- 03-25-2011, 03:17 AM #6
Member
- Join Date
- Feb 2011
- Location
- NY
- Posts
- 5
- Rep Power
- 0
DUH! Thank you DB. It compiled successfully but gave me a note:
Note: DeckOfCards.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
What is the meaning of this?
- 03-25-2011, 03:55 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
This means you didn't use generics. Note: it's a warning, not an error. Technically it can run, but should be done better. Here is an example of employees and a compony to demonstrate arrayList.
This code doesn't do much that's useful and is really just the beginning of a classes design. However, it shows how to add items to a list, and it shows generics as well.Java Code:class Employee{ private int id; private int salary; public Employee(int id){ this.id = id; this.salary = id * 10; } } public class Company{ private ArrayList<Employee> employees = new ArrayList<Employee>(); //note the generics in the <>, it tells the list that it contains the type inside the <> public Company(int size){ for(int i = 0; i < size; i++){ Employee e = new Employee(i + 1); employees.add(e); } } }
It would be really helpful to have another constructor in the card class that will take a single number and create each card like this
Java Code:public class Deck{ //declare arraylist public Deck(){ for(int i = 0, i < 52; i++){ Card c = new Card(i + 1); ArrayList.add(c); } } }
Similar Threads
-
Need Help in Removing Objects from list
By pramod_r20 in forum New To JavaReplies: 2Last Post: 12-10-2010, 12:19 PM -
add objects to list
By javaMike in forum Advanced JavaReplies: 1Last Post: 11-17-2009, 10:11 PM -
List of objects
By bubbless in forum New To JavaReplies: 0Last Post: 08-01-2009, 04:21 PM -
List of objects
By rekha in forum New To JavaReplies: 6Last Post: 03-20-2009, 11:39 AM -
Getting objects from a list
By markyoung1984 in forum New To JavaReplies: 4Last Post: 03-13-2008, 10:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks