954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mutiple deck of cards inside a array

lets say i have 8 deck of cards how to put all of that inside a array do i have to make a multidimensional array for each deck???

ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

Depends on how you define/store a Deck.
Ideally Deck will be a class, so you can just have an array of them:

Deck[] myDecks = new Deck[8];  // create array
for (int 1 = 0; i < myDecks.length; i++) {
   myDecks[i] = new Deck(...); // initialise array
}

but if your deck is just a crummy array of Cards the you will need a 2D array

Card[][] myDecks = new Card[8][52];
// .. and initialise it all
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

You can also use an ArrayList, or an ArrayList>. I like arrayLists because of the easy manipulation and editing of the contents.

ArrayList<Deck> deckAL= new ArrayList<Deck>();
for (int i = 0; i < 8; i++) {
   deckAL.add(new Deck(...));
}


Or:

ArrayList<ArrayList<Card>> cardAL= new ArrayList<ArrayList<Card>>();
for (int i = 0; i < 8; i++) {
   deckAL.add(new ArrayList<Card>());
}
for (ArrayList<Card> a: cardAL) {
   a.add(new Card(...));
}
hiddepolen
Posting Whiz in Training
297 posts since Oct 2010
Reputation Points: 82
Solved Threads: 35
 

^ yes.
For some bizarre reason teachers use a syllabus based on very traditional programming that insist on torturing students with naked arrays before letting them use any more useful collection classes. IMHO that's a distraction because they spend more time struggling with uninitialised array elements, trying to guess a size for the array, etc rather than thinking about algorithms and code structure.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: