Results 1 to 5 of 5
Thread: Object name by string variable?
- 10-14-2009, 01:59 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
Object name by string variable?
Hello, I am making a class/program that will create a deck of cards for me.
However, I have a method that creates more then one deck ,if the user means to.
It goes like this.
card[] deck1 = new Card[47]; (47 is the amount of cards in a deck).
What i wish to do , is to allow my method to be able to change the object name, so i can make different decks (aka deck2, deck3), via using a loop
However, it does not seem to like me doing this.. is there any way possible?Java Code:createDeck(int NumberOfDecks){ for(int i = 0;i <= NumberOfDecks; i++){ card[] deck + i = new Card[47]. }
If not, I could just create a number of decks in the same array, and keep track of which cards belong to each deck, but this is a very messy way of solving a problem like this.Last edited by zerkz; 10-14-2009 at 02:10 AM.
- 10-14-2009, 03:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You don't have variables that are dynamic like this is Java.
Use a double array of Card. (where "array" could be a List.)
- 10-14-2009, 03:19 AM #3
Usually, a method would create 1 deck. Then, using it, you could create multiple decks from it. This allows you to store the multiple decks in a collection of your choice, or an array. Also, the decks could contain a different number of cards.
However, if you want to store multiple decks, choose a collection/arary and store them.
P.S. If you're creating a deck, check out the shuffle method in the Collections interfaceJava Code:Deck[] decks = new Deck[numberOfDecks]; for (int i = 0; i < numberOfDecks; i++) { decks[i] = createDeck(numberOfCards); } return decks;CodesAway - codesaway.info
writing tools that make writing code a little easier
- 10-14-2009, 03:33 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
- 10-14-2009, 07:16 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Java Code:Card[][] createDeck(int numberOfDecks) { return new Card[numberOfDecks][47]; }
This just creates (and returns) the right number of packs. Like your example it does not actually put anything into the packs.
For the example the third deck out of 10 might be accessed with:
Java Code:Card[][] packArr = createDeck(10); // ... System.out.println(packArr[3]); // prints the third pack // puts something ito the third pack for(indx = 0 ndx < 47; ndx++) { packArr[3][ndx] = new Card(/*some argument*/); }
Similar Threads
-
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 06:32 AM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:43 AM -
variable to accept a single object
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:41 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 04:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks