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???

Recommended Answers

All 3 Replies

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

You can also use an ArrayList<Deck>, or an ArrayList<ArrayList<Card>>. 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(...));
}

^ 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.