Billll Gatess
Billll Gatess

Reputation: 11

Making no duplicates with Arraylist in Java

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

Upvotes: 1

Views: 5522

Answers (3)

Alex Vizzone
Alex Vizzone

Reputation: 128

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.

Upvotes: 3

Bohemian
Bohemian

Reputation: 424983

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.

Upvotes: 8

Stephen C
Stephen C

Reputation: 718698

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!)

Upvotes: 1

Related Questions