Results 1 to 3 of 3
  1. #1
    linc186 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Card deck arrays?

    So i made an array that was a complete deck of cards. But, i need the program to randomly take five cards in the whole array. How would i do that? I tried the random type but apparently string is undefined for it. What would i do?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Shuffle the array (generate nth pairs of random numbers from 0 to array.lenght-1 and swap the elements on these positions ) and take the first 5 array elements! :)
    Or use a list instead of an array and use the Collections.shuffle() method !

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Or use a list instead of an array and use the Collections.shuffle() method !

    I agree as this would be much easier to process.

    If you want to convert your array to a list:

    Java Code:
    java.util.List cardDeck = new java.util.ArrayList();
    cardDeck.addAll(java.util.Arrays.asList(myCardDeckArray));

    Then shuffle it:
    Java Code:
    java.util.Collections.shuffle(cardDeck);

    Then use the first 5 elements/objects:
    Java Code:
    Card card1 = cardDeck.get(0);
    Card card2 = cardDeck.get(1);
    Card card3 = cardDeck.get(2);
    Card card4 = cardDeck.get(3);
    Card card5 = cardDeck.get(4);

Similar Threads

  1. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 04:45 AM
  2. Deck Shuffle
    By Cragsterboy in forum New To Java
    Replies: 1
    Last Post: 12-13-2010, 04:23 PM
  3. Replies: 2
    Last Post: 12-13-2010, 11:12 AM
  4. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  5. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM

Posting Permissions

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