Java 随机数生成器到数组,没有重复项

Java 随机数生成器到数组,没有重复项,java,arrays,random,Java,Arrays,Random,因此,我试图制作一个程序,生成一个由20个随机数组成的数组,该数组没有重复项(对于最终用户)。这是到目前为止我的代码 import java.util.*; public class randomprog { public static void main(String args[]) { Random rand = new Random(); int[] list = new int[20]; boolean generating=true; int

因此,我试图制作一个程序,生成一个由20个随机数组成的数组,该数组没有重复项(对于最终用户)。这是到目前为止我的代码

import java.util.*;
public class randomprog
{
public static void main(String args[])     
{
    Random rand = new Random();
    int[] list = new int[20];
    boolean generating=true;
    int counting=0;
    while(generating)
    {
        int testNum= rand.nextInt(30)+1;
        if (Arrays.asList(list).contains(testNum))
        {}
        else
        {
            list[counting]=testNum;
            counting++;
            System.out.println(testNum);
        }
        if(counting>=20)
        {
            generating=false;
        }
    }
}}
正如您所看到的,我已经尝试使用Arrays.asList(list).contains(mynumber),但是我仍然在我的输出中接收到重复项,如 29 4. 4. 1. 20 30 20 23 30 11 6. 7. 27 14 16 8. 4. 19 7. 十五


有什么建议吗?

使用哈希集跟踪您使用的数字

比如说

int[] result = new int[20];

HashSet<Integer> used = new HashSet<Integer>();
for (int i = 0; i < 20; i++) {
    int add = (int)(Math.random() * 30); //this is the int we are adding
    while (used.contains(add)) { //while we have already used the number
        add = (int) (Math.random() * 30); //generate a new one because it's already used
    }
    //by this time, add will be unique
    used.add(add);
    result[i] = add;
}
int[]结果=新的int[20];
HashSet used=新的HashSet();
对于(int i=0;i<20;i++){
int add=(int)(Math.random()*30);//这是我们要添加的int
while(used.contains(add)){//while我们已经使用了这个数字
add=(int)(Math.random()*30);//生成一个新的,因为它已被使用
}
//此时,add将是唯一的
使用。添加(添加);
结果[i]=相加;
}

这样可以确保没有重复项,并且比在ArrayList中搜索快得多,每次搜索数字时,ArrayList将执行与ArrayList大小相等的许多操作。当您检查是否包含数字时,哈希集仅执行1个操作。

使用哈希集跟踪您使用的数字

比如说

int[] result = new int[20];

HashSet<Integer> used = new HashSet<Integer>();
for (int i = 0; i < 20; i++) {
    int add = (int)(Math.random() * 30); //this is the int we are adding
    while (used.contains(add)) { //while we have already used the number
        add = (int) (Math.random() * 30); //generate a new one because it's already used
    }
    //by this time, add will be unique
    used.add(add);
    result[i] = add;
}
int[]结果=新的int[20];
HashSet used=新的HashSet();
对于(int i=0;i<20;i++){
int add=(int)(Math.random()*30);//这是我们要添加的int
while(used.contains(add)){//while我们已经使用了这个数字
add=(int)(Math.random()*30);//生成一个新的,因为它已被使用
}
//此时,add将是唯一的
使用。添加(添加);
结果[i]=相加;
}

这样可以确保没有重复项,并且比在ArrayList中搜索快得多,每次搜索数字时,ArrayList将执行与ArrayList大小相等的许多操作。当检查是否包含数字时,哈希集只执行1个操作。

我建议使用ArrayList,不要使用空if块。 这应该行得通

   import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    public class Main {
            static List list = new ArrayList<>(20);
            public static void main(String args[])
            {
                Random rand = new Random();
                boolean generating=true;
                int counting=0;
                while(generating)
                {
                    int testNum= rand.nextInt(30)+1;
                    if (!list.contains(testNum))
                    {
                        list.add(testNum);
                        counting++;
                        System.out.println(testNum);
                    }
                    if(counting>=20)
                    {
                        generating=false;
                    }
                }
            }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
公共班机{
静态列表=新的ArrayList(20);
公共静态void main(字符串参数[])
{
Random rand=新的Random();
布尔生成=真;
整数计数=0;
同时(生成)
{
int testNum=rand.nextInt(30)+1;
如果(!list.contains(testNum))
{
添加(testNum);
计数++;
System.out.println(testNum);
}
如果(计数>=20)
{
生成=假;
}
}
}
}

我建议使用ArrayList,不要使用空if块。 这应该行得通

   import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    public class Main {
            static List list = new ArrayList<>(20);
            public static void main(String args[])
            {
                Random rand = new Random();
                boolean generating=true;
                int counting=0;
                while(generating)
                {
                    int testNum= rand.nextInt(30)+1;
                    if (!list.contains(testNum))
                    {
                        list.add(testNum);
                        counting++;
                        System.out.println(testNum);
                    }
                    if(counting>=20)
                    {
                        generating=false;
                    }
                }
            }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
公共班机{
静态列表=新的ArrayList(20);
公共静态void main(字符串参数[])
{
Random rand=新的Random();
布尔生成=真;
整数计数=0;
同时(生成)
{
int testNum=rand.nextInt(30)+1;
如果(!list.contains(testNum))
{
添加(testNum);
计数++;
System.out.println(testNum);
}
如果(计数>=20)
{
生成=假;
}
}
}
}

代码不起作用的原因是
数组。asList(int[]list)
返回大小为1的
ArrayList
,而不是
ArrayList
。因此,当您调用
contains
时,它不会检查原始列表的整数元素,并且总是返回
false

代码不起作用的原因是
数组。asList(int[]list)
返回大小为1的
ArrayList
,而不是
ArrayList
。因此,当您调用
contains
时,它不会检查原始列表的整数元素,并且总是返回
false

使用集合。Shuffle

class Ideone{
    public static void main(String[] args) {
    Integer[] arr = new Integer[20];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i;
    }
    Collections.shuffle(Arrays.asList(arr));
    System.out.println(Arrays.toString(arr));

}}
类表意符{
公共静态void main(字符串[]args){
整数[]arr=新整数[20];
对于(int i=0;i
使用集合。洗牌

class Ideone{
    public static void main(String[] args) {
    Integer[] arr = new Integer[20];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i;
    }
    Collections.shuffle(Arrays.asList(arr));
    System.out.println(Arrays.toString(arr));

}}
类表意符{
公共静态void main(字符串[]args){
整数[]arr=新整数[20];
对于(int i=0;i
洗牌数组将如何删除重复项?洗牌不只是将顺序随机化吗?他问道:“所以我试图制作一个程序,生成一个由20个随机数组成的数组,该数组(对最终用户而言)没有重复数。”洗牌不会删除重复数。然后,该数组将以随机顺序包含从0到19的所有整数。所以,是的,这很可能就是我们所要求的。我同意你的看法。但他要求他需要创建一个由20个随机数组成的数组,这些随机数本身不会重复。上面的代码片段将完成这项工作。洗牌数组将如何删除重复项?洗牌不只是随机排列顺序吗?他问道:“所以我试图制作一个程序,生成一个由20个随机数组成的数组,该数组(对最终用户而言)没有重复项。”洗牌不会删除重复项。然后,该数组将