Results 1 to 5 of 5
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default 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
    Java Code:
    createDeck(int NumberOfDecks){
    for(int i = 0;i <= NumberOfDecks; i++){
    card[] deck + i = new Card[47].
    }
    However, it does not seem to like me doing this.. is there any way possible?

    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You don't have variables that are dynamic like this is Java.

    Use a double array of Card. (where "array" could be a List.)

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    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.

    Java Code:
    Deck[] decks = new Deck[numberOfDecks];
    
    for (int i = 0; i < numberOfDecks; i++)
    {
    	decks[i] = createDeck(numberOfCards);
    }
    
    return decks;
    P.S. If you're creating a deck, check out the shuffle method in the Collections interface
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You don't have variables that are dynamic like this is Java.

    Use a double array of Card. (where "array" could be a List.)
    How would one use a double array?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by zerkz View Post
    How would one use a double array?
    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

  1. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 06:32 AM
  2. Help with variable assigment to String
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:43 AM
  3. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 AM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 04:30 AM

Posting Permissions

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