Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I am trying to shuffle up an array

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default I am trying to shuffle up an array

    ...................................
    Last edited by rsala004; February 11th, 2011 at 04:37 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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);
            }

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    public void shuffle(Card[] fulldeck)
    {
        Random gen = new Random();
        for (int i = 0; i < fulldeck.length; i++)
        {
            int index = gen.nextInt(fulldeck.length);
            Card temp = fulldeck[index];
            fulldeck[index] = fulldeck[i];
            fulldeck[i] = temp;
        }
    }

  4. #4
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: I am trying to shuffle up an array

    ah i understand , thanks

    thats much better
    Last edited by rsala004; July 18th, 2009 at 03:46 PM.