1

ok so i am new to ArrayList, what i am trying to do is make a program that gets 3 random cards from a deck of 54 cards WITHOUT any duplicates. i dont know what to put inside of my if loops. Please help

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.ArrayList;

public class card_ran1 extends JFrame
{
    public card_ran1()
    {
        ArrayList<Integer> Ran = new ArrayList<Integer>();
        setLayout(new GridLayout(1,4,5,5));
        Random random = new Random();
        int i = random.nextInt(54) + 1 ;
        int n = random.nextInt(54) + 1 ;
        int m = random.nextInt(54) + 1 ;
        Ran.add(i);

        if (Ran.contains(n))
        {
            //what should go here
        }
        if (Ran.contains(m)) 
        {
            //what should go here
        }
        add(new JLabel(new ImageIcon("card/" + Ran.get(0) + ".png")));
        add(new JLabel(new ImageIcon("card/" + Ran.get(1) + ".png")));
        add(new JLabel(new ImageIcon("card/" + Ran.get(2) + ".png")));
    }

    public static void main(String[] args) 
    {
        card_ran1 frame = new card_ran1();
        frame.setTitle("Random Cards");
        frame.setSize(600,300);
        frame.setLocationRelativeTo( null );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
    }
}
5
  • I think you should use do { ... } while ( ... ); rather than if in this case since you'd want to keep trying to get a new card until you get a non-duplicate. Commented Aug 29, 2013 at 1:25
  • 5
    Another approach would be to shuffle the deck and take the first 3 cards. Commented Aug 29, 2013 at 1:26
  • 1
    I agree with @NormR, Collections.shuffle followed but sublist(0,3). Commented Aug 29, 2013 at 1:34
  • Why don't you use Set instead of ArrayList, this will assure you that there will be no duplication. Commented Aug 29, 2013 at 1:39
  • @JjTuibeo - The order in which the cards are drawn from the deck could be significant. Most Set implementations don't represent the insertion order. Commented Aug 29, 2013 at 1:45

3 Answers 3

8

If the values are unique, use a Set, not a List.

Set<Integer> set = new LinkedHashSet<Integer>(); // order is preserved

Use a loop to get 3 different values:

while (set.size() < 3)
    set.add(random.nextInt(54) + 1);

If you really need a List, use the copy constructor:

List<Integer> list = new ArrayList<Integer>(set);

The whole thing can be done in just a few lines of code.

Sign up to request clarification or add additional context in comments.

3 Comments

This may be the best approach, but IMO the OP also needs to understand how to do this with an ArrayList.
What would happen if random.nextInt(54) happens to return almost infinite times the same value? As you said, random is random
@MostyMostacho while theoretically possible, more than even a mere few collisions is statistically "impossible". Think about it... just 4 consecutive collisions is already 8 million to one. 50 in a row is a number greater than the estimated number if particles in the universe. Each attempt is a few microseconds. It's not a problem.
3

As for your question, you can remove an item (or card in your case) with the following method: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove(int)

As for a loop, I'm not entirely sure what you are trying to accomplish. If you decide to create a loop and pick your card and then remove it in each iteration then it will work. Currently, you will be unable to properly remove your cards from the deck because their indices will be incorrect when you remove the first due to the size of the ArrayList shrinking. Create your variables outside the loop so you don't lose your information.

Comments

1

Hints:

  • Each time you get a new random number, it could already be in the ArrayList. So use a loop!

You could also use a Set, which doesn't allow duplicates ... but that has the problem of not preserving the order of the randomly selected "cards". If you need to preserve the order, use a LinkedHashSet.


You have a number of style errors in your code:

  • A Java class name should be "camel case" starting with a uppercase letter. On this basis, test_ran1 should be TestRan1.

  • A Java variable name should be "camel case". On this basis, Ran should be ran.

  • Cute abbreviations are a bad idea, especially in classnames. The name should be TestRandom.

  • Your code's indentation is a mess. You should consistently indent by the same number of spaces according to the block structure of the code. Look at other examples.

(If this code is going to be marked, your marker should mark you down for these things! If I was setting the marking scheme, you'd lose all style marks for code that looked like that!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.