Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java生成无重复的数字而不使用arraylist_Java_Arraylist_Shuffle - Fatal编程技术网

java生成无重复的数字而不使用arraylist

java生成无重复的数字而不使用arraylist,java,arraylist,shuffle,Java,Arraylist,Shuffle,我知道数组列表洗牌的常用方法; 有没有其他方法可以模仿洗牌对数组(而不是列表)的作用? 我已经编写了一些与rand.NextInt和IF和while一起工作的代码; 但我高度怀疑这是最有效的方法。 考虑到低资源有什么想法吗 这就是我得到的;但我怀疑效率: static int[] order = new int[] {1,2,3,4}; static int[] order2 = new int[] {0,0,0,0,0}; static int p;

我知道数组列表洗牌的常用方法; 有没有其他方法可以模仿洗牌对数组(而不是列表)的作用? 我已经编写了一些与
rand.NextInt
IF
while
一起工作的代码; 但我高度怀疑这是最有效的方法。 考虑到低资源有什么想法吗

这就是我得到的;但我怀疑效率:

       static int[] order = new int[] {1,2,3,4};
       static int[] order2 = new int[] {0,0,0,0,0};
       static int p;
        Random rand = new Random(); 
    int m=global.order[rand.nextInt( global.order.length)];
    global.order2[m]=m;

    int n=global.order[rand.nextInt( global.order.length)];
    while ( m==n )  { n=global.order[rand.nextInt( global.order.length)];}
    global.order2[n]=n;

    int o=global.order[rand.nextInt( global.order.length)];

    while (m==o || n==o) { o=global.order[rand.nextInt( global.order.length)]; }
    global.order2[o]=o;
    for (int y=1; y<=4; y++){
        if (global.order2[y]!=m && global.order2[y]!=n && global.order2[y]!=o){
            global.order2[y]=y;
            global.p=y;
static int[]order=new int[]{1,2,3,4};
静态int[]order2=新int[]{0,0,0,0};
静态int-p;
Random rand=新的Random();
intm=global.order[rand.nextInt(global.order.length)];
全局.order2[m]=m;
intn=global.order[rand.nextInt(global.order.length)];
而(m==n){n=global.order[rand.nextInt(global.order.length)];}
全局.order2[n]=n;
into=global.order[rand.nextInt(global.order.length)];
而(m==o | | n==o){o=global.order[rand.nextInt(global.order.length)];}
全局.order2[o]=o;

对于(int y=1;y),如果需要的是一个未排序的集合,它具有随机生成的值,请考虑使用哈希集。

要在普通数组中实现这一点,插入数组将花费
O(n)
时间,因为您需要查看所有元素以查看它是否已经包含随机数。使用
HashSet
插入将花费
O(1)
。然后可以将其转换为数组

import java.util.Arrays;
import java.util.HashSet;

public class MakeUnsortedSet {

    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();

        while (set.size() < 10) {
            set.add((int)(Math.random() * 20)); 
        }

        Integer[] ints = new Integer[10];
        set.toArray(ints);

        System.out.println(Arrays.toString(ints));
    }
}
导入java.util.array;
导入java.util.HashSet;
公共类makensortedset{
公共静态void main(字符串[]args){
HashSet=newhashset();
while(set.size()<10){
set.add((int)(Math.random()*20));
}
整数[]整数=新整数[10];
设置到阵列(整数);
System.out.println(Arrays.toString(ints));
}
}

本质上,
ArrayList
只是一个常规数组,包装在
List
接口实现中。如果您认为可以找到更好的实现来满足您的性能需求,只需实现您自己的
List
并将其传递给
集合。shuffle()

如果要避免为每个数组实例化
列表
实现中的新对象,可以通过这样的方式来实现它:您只能替换对包装数组的引用并进行少量修改

无论如何,我会先看看
ArrayList
的源代码(也许
会扩展它),然后再尝试重新发明轮子。

使用Fisher-Yates shuffle()

您可以很容易地按照以下准则创建自己的Java实现:

  • 从范围
    [0..array.length)
  • 交换
    数组[0]
    数组[r]
  • 从范围
    [1..array.length)
  • 交换
    数组[1]
    数组[r]

  • 依此类推。这很容易转化为循环。性能很好,不需要重新分配数组或创建新对象,执行时间可以线性扩展。

    我完全不明白您想问什么。您的目标是什么?为什么
    Collections.shuffle()
    不够好?你想要一个没有重复项的随机数数组吗?像集合一样?假设我们有一个数组{1,2,3,4};如何以最有效的方式洗牌;而不将其强制转换为列表?@robbmj我已经拥有了该集;我想洗牌。如果不强制转换,则必须采用一种不麻烦的方式。我想最麻烦的方式是将数组传递给
    ArrayList
    的构造函数。如果不深入研究源代码,我会猜这是ay的性能相当便宜,当然是最简单的。@Shervin4030,是的!Fisher Yates实际上就是
    集合使用的东西。shuffle
    使用的(尽管Java自己的版本是相反的,将
    列表从头到尾洗牌)但我也理解你对就地数组版本的渴望。这是所谓的FY算法的现代方式吗?还是通常的方式?@Shervin4030 IIRC“现代”是指使用交换而不是复制的FY实现,所以是的,这是现代的。Wikipedia的现代示例似乎也从头到尾都有,但方向是正确的(开始到结束vs结束到开始)并不是很重要。有时候维基百科更像是lostopedia;tnx我得到了我想要的needed@Shervin4030,没问题,很高兴能帮上忙。