简体   繁体   English

在Java中使用Arraylist进行重复

[英]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. 好的,所以我是ArrayList的新手,我要做的是制作一个程序,该程序从54张牌中抽取3张随机牌,且不重复。 i dont know what to put inside of my if loops. 我不知道该放在我的if循环里面。 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 );
    }
}

If the values are unique, use a Set , not a List . 如果值是唯一的,请使用Set而不是List

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

Use a loop to get 3 different values: 使用循环获取3个不同的值:

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

If you really need a List , use the copy constructor: 如果您确实需要List ,请使用复制构造函数:

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

The whole thing can be done in just a few lines of code. 只需几行代码即可完成整个过程。

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) 关于您的问题,您可以使用以下方法删除商品(或您的卡片): 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. 当前,您将无法从卡座中正确删除卡,因为由于ArrayList的大小缩小,当删除第一个卡时,它们的索引将不正确。 Create your variables outside the loop so you don't lose your information. 在循环外创建变量,以免丢失信息。

Hints: 提示:

  • Each time you get a new random number, it could already be in the ArrayList . 每次您获得一个新的随机数时,它可能已经在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". 您也可以使用一个Set,它不允许重复...但是存在不能保留随机选择的“卡片”顺序的问题。 If you need to preserve the order, use a LinkedHashSet . 如果需要保留订单,请使用LinkedHashSet


You have a number of style errors in your code: 您的代码中存在许多样式错误:

  • A Java class name should be "camel case" starting with a uppercase letter. Java类名称应为“驼峰式”,以大写字母开头。 On this basis, test_ran1 should be TestRan1 . 在此基础上, test_ran1应该是TestRan1

  • A Java variable name should be "camel case". Java变量名称应为“ camel case”。 On this basis, Ran should be ran . 在此基础上,应该ran Ran

  • Cute abbreviations are a bad idea, especially in classnames. 可爱的缩写是个坏主意,尤其是在类名中。 The name should be TestRandom . 名称应为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!) (如果要标记此代码,则标记器应将这些标记为您!如果我设置标记方案,则将丢失看起来像这样的代码的所有样式标记!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM