# Thread: How do you shuffle an ArrayList?

1. ## How do you shuffle an ArrayList?

How do you shuffle an ArrayList?

Currently, I got this code:

Code:
```import java.util.ArrayList;
import cs1.Keyboard;
import java.util.Random;

public class DeckOfCards
{
private static int totalCards = 52;
public static ArrayList deck = new ArrayList(0);
public static Random generator = new Random();

static void deal()
{
totalCards--;
int randNum = generator.nextInt(50) + 1;
System.out.println("Card Dealt: " + deck.get(randNum));
}

static void shuffle();
{
totalCards = 52;
//wtf happens here?? lol.

}

public static void main(String[] args)
{

String dealChoice;
String shuffleChoice;

String keepPlaying = "y";

Card card1;
Card card2;
Card card3;
Card card4;
Card card5;
Card card6;
Card card7;
Card card8;
Card card9;
Card card10;
Card card11;
Card card12;
Card card13;
Card card14;
Card card15;
Card card16;
Card card17;
Card card18;
Card card19;
Card card20;
Card card21;
Card card22;
Card card23;
Card card24;
Card card25;
Card card26;
Card card27;
Card card28;
Card card29;
Card card30;
Card card31;
Card card32;
Card card33;
Card card34;
Card card35;
Card card36;
Card card37;
Card card38;
Card card39;
Card card40;
Card card41;
Card card42;
Card card43;
Card card44;
Card card45;
Card card46;
Card card47;
Card card48;
Card card49;
Card card50;
Card card51;
Card card52;

card1 = new Card(1, 1);
card2 = new Card(2, 1);
card3 = new Card(3, 1);
card4 = new Card(4, 1);
card5 = new Card(5, 1);
card6 = new Card(6, 1);
card7 = new Card(7, 1);
card8 = new Card(8, 1);
card9 = new Card(9, 1);
card10 = new Card(10, 1);
card11 = new Card(11, 1);
card12 = new Card(12, 1);
card13 = new Card(13, 1);
card14 = new Card(1, 2);
card15 = new Card(2, 2);

card16 = new Card(3, 2);
card17 = new Card(4, 2);
card18 = new Card(5, 2);
card19 = new Card(6, 2);
card20 = new Card(7, 2);
card21 = new Card(8, 2);
card22 = new Card(9, 2);
card23 = new Card(10, 2);
card24 = new Card(11, 2);
card25 = new Card(12, 2);
card26 = new Card(13, 2);
card27 = new Card(1, 3);
card28 = new Card(2, 3);
card29 = new Card(3, 3);
card30 = new Card(4, 3);
card31 = new Card(5, 3);
card32 = new Card(6, 3);
card33 = new Card(7, 3);
card34 = new Card(8, 3);
card35 = new Card(9, 3);
card36 = new Card(10, 3);
card37 = new Card(11, 3);
card38 = new Card(12, 3);
card39 = new Card(13, 3);
card40 = new Card(1, 4);
card41 = new Card(2, 4);
card42 = new Card(3, 4);
card43 = new Card(4, 4);
card44 = new Card(5, 4);
card45 = new Card(6, 4);
card46 = new Card(7, 4);
card47 = new Card(8, 4);
card48 = new Card(9, 4);
card49 = new Card(10, 4);
card50 = new Card(11, 4);
card51 = new Card(12, 4);
card52 = new Card(13, 4);

//Clubs

//Diamonds

//Hearts

do {
// *** print the contents of the cart object using println
System.out.print("Deal? (y/n): ");
if (dealChoice.equals("y"))
{
deal();
}
else{
System.out.print("Shuffle Deck? (y/n): ");
if (shuffleChoice.equals("y"))
{
shuffle();
}
else
{
System.out.println("Bah.");
}
}

System.out.print ("Continue shopping (y/n)? ");

} while (keepPlaying.equals("y"));
}
}```
Kind regards,
Jaime Bueza
Last edited by BuezaWebDev; 11-21-2004 at 12:28 AM.

2. Super Moderator
Join Date
Jan 2004
Location
Melbourne, Australia
Posts
5,298
Woah!

I would suggest you setup an array of Cards, rather than 52 seperate Cards. Or load the data from a seperate flat file

It would be best to actually subclass ArrayList or List and make your own Deck object.

There is many ways to shuffle the Deck. You could generate a few random numbers (between 1 to 52), which will be used to deal the next bunch of cards. Or you could actually devise some type of formula to grab random indexes and swap them over.

Regards.

3. Hrmm, the instructions specifically say to make a deck of 52 card objects. :O

So, am I doing that correctly? Because I have a knack to not follow directions properly. Heheh.

Hrm, about that formula to swap a few cards in and out. Hrm. Argh, that's hard to think of. I wish the ArrayList class had a method to randomize the indexes.

Hehe, thanks buntine!

Kind regards,
J

4. Registered User
Join Date
Nov 2004
Posts
29
that is a lot of code. holy night. If i get a chance later i will post the code that is in my java book, that has code for shuffling cards. And no offense, but its a lil easier to read, and smaller

5. Super Moderator
Join Date
Jan 2004
Location
Melbourne, Australia
Posts
5,298
What I like to do is write two versions of the assignment or task; one to the lecturer's specifications and one to my specifications. Pretty sad, but it seems to pay off.

Regards.

6. Just because they said to make a deck of 52 card objects does not mean you have to write it like
Code:
```Card card1;
Card card2;
...

card1=new Card(1, 1);
card2=new Card(1, 2);
...```
You could make it like
Code:
```ArrayList deck=new ArrayList(); //the deck
Card cards[]=new Card[52]; //array to keep the cards in
for(int i=0; i<4; i++) {
for(int j=0; j<13; j++) {
cards[i*13+j]=new Card(j+1, i+1); //create the card
}
}```
In that manner, your card1 is the same as cards[0], and so on. As for randomizing it, perform a bunch of random swaps
Code:
```for(int i=0; i<26; i++) {
//choose 2 random elements to swap
int swapIndex1=(int)Math.floor(Math.random()*52);
if(swapIndex1==52) swapIndex1--;
int swapIndex2=(int)Math.floor(Math.random()*52);
if(swapIndex2==52) swapIndex2--;

//swap them
Object tmp=deck.get(swapIndex1);
deck.set(swapIndex1, deck.get(swapIndex2));
deck.set(swapIndex2, tmp);
}```

7. BuezaWebDev, implementing a shuffle without keeping all the Card objects in an array is going to be damn near impossible.

Though I would implement the shuffle in this way ...
Code:
```protected static final Random generator = new Random();

protected static void shuffle(Object[] a)
{
int i, j;

for (i = 0; i < a.length; ++i) {
j = generator.nextInt(a.length);
swap(a, i, j);
}
}

protected static void swap(Object[] a, int m, int n)
{
Object t;

t = a[m];
a[m] = a[n];
a[n] = t;
}```
This method of shuffling iterates a number of times proportional to the size of the array, and it guarentees that every element is swapped at least once.

8. Holy smokes!

You guys are the BEST!

9. You know what, my friend just told me that there's a ShuffleList method from the Collections java package. :|

!!