The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: I am trying to shuffle up an array

Hello and welcome,

Most of this code is just initialising the cards list but here we go.

final List<String> cards = new ArrayList<String>();
cards.add("Ace of Diamonds");
cards.add("One of Diamonds");
cards.add("Two of Diamonds");
cards.add("Three of Diamonds");
cards.add("Four of Diamonds");
cards.add("Five of Diamonds");
cards.add("Six of Diamonds");
cards.add("Seven of Diamonds");
cards.add("Eight of Diamonds");
cards.add("Nine of Diamonds");
cards.add("Ten of Diamonds");
cards.add("Knight of Diamonds");
cards.add("Queen of Diamonds");
cards.add("King of Diamonds");
cards.add("Ace of Spades");
cards.add("One of Spades");
cards.add("Two of Spades");
cards.add("Three of Spades");
cards.add("Four of Spades");
cards.add("Five of Spades");
cards.add("Six of Spades");
cards.add("Seven of Spades");
cards.add("Eight of Spades");
cards.add("Nine of Spades");
cards.add("Ten of Spades");
cards.add("Knight of Spades");
cards.add("Queen of Spades");
cards.add("King of Spades");
cards.add("Ace of Hearts");
cards.add("One of Hearts");
cards.add("Two of Hearts");
cards.add("Three of Hearts");
cards.add("Four of Hearts");
cards.add("Five of Hearts");
cards.add("Six of Hearts");
cards.add("Seven of Hearts");
cards.add("Eight of Hearts");
cards.add("Nine of Hearts");
cards.add("Ten of Hearts");
cards.add("Knight of Hearts");
cards.add("Queen of Hearts");
cards.add("King of Hearts");
cards.add("Ace of Clubs");
cards.add("One of Clubs");
cards.add("Two of Clubs");
cards.add("Three of Clubs");
cards.add("Four of Clubs");
cards.add("Five of Clubs");
cards.add("Six of Clubs");
cards.add("Seven of Clubs");
cards.add("Eight of Clubs");
cards.add("Nine of Clubs");
cards.add("Ten of Clubs");
cards.add("Knight of Clubs");
cards.add("Queen of Clubs");
cards.add("King of Clubs");
Collections.shuffle(cards);
for (final String card : cards) {
System.out.println(card);
}

Re: I am trying to shuffle up an array

Rasla, there's a flaw in your shuffling algorithm. Firstly, you never update taken, so it is possible (and very likely) to have duplicate cards end up in the shuffled deck. Secondly, you're using an awful amount of space. A better sorting algorithm would be to keep swapping cards.