在数组中存储非重复数字

时间:2015-02-02 10:28:23

标签: java arrays random boolean

概述: 我的程序必须在阵列中存储20个非重复的随机生成的数字,范围从1到100。 问题: 如果我在内部for循环中找到匹配(重复的#),我标记布尔变量。这是我不知道该怎么做。在for循环之后,如果没有标记布尔值,我想将随机数添加到数组中。我还想增加x(直到我存储了20个非重复数字),但前提是我将元素添加到数组中。

public void duplication(){
    int max = 100; // max value for range
    int min = 1; // min value for range
    boolean duplicate = false;
    Random rand = new Random();

    for (int x = 0; x < 20; x++){
        //initiates array that stores 20 values
        int[] all = new int[20];
        //generates # from 1-100
        int randomNum = rand.nextInt((max - min) + 1) + min;
        all[x] = randomNum;
        //iterates through array
        for (int i : all) {
            //if there's a match (duplicate) flag boolean
            if (i == randomNum){
                duplicate = true;
            }
            else {
                duplicate = false;
            }
        }
    }
    //if boolean hasn't been flagged
    if (duplicate=false){
        //store to array
    }
}   

3 个答案:

答案 0 :(得分:5)

这将为您提供0到100之间的随机整数数组

public static void main(String[] args) {
    Set<Integer> s = new HashSet<Integer>(); // create a set
    Random r = new Random();
    while (s.size() < 20) {
        s.add(r.nextInt(100)+1);  // add elements to set upto size =20
    }
    Integer[] array = new Integer[20];
    s.toArray(array); // convert set to array of Integers
    System.out.println(Arrays.toString(array));

}

O / P:

[68, 69, 64, 65, 66, 67, 47, 74, 14, 17, 16, 21, 81, 54, 52, 82, 59, 57, 63, 88]

答案 1 :(得分:1)

首先需要在循环之前初始化数组,其次,不要在第二个循环中将副本设置为false,因为最后一个数字可以将其设置为false,即使之前存在重复。最后一个,只有在没有找到副本的情况下设置值,如果duplicate为真,则设置x值。

public void duplication(){
    int max = 100; // max value for range
    int min = 1; // min value for range
    boolean duplicate = false;
    Random rand = new Random();

    //initiates array that stores 20 values
    //you need to init it before the loop!
    int[] all = new int[20];
    for (int x = 0; x < 20; x++){
        duplicate = false;
        //generates # from 1-100
        int randomNum = rand.nextInt((max - min) + 1) + min;
        //iterates through array
        for (int i : all) {
            //if there's a match (duplicate) flag boolean
            if (i == randomNum){
                duplicate = true;
                //we can break the loop here
                break;
            }
        }
        //if boolean is true, just stay at the same x value(for loop will increase by 1, thats why we decrese it by 1 here)
        if (duplicate){
            x--;
        } else {
            //if everything is ok, set the number
            all[x] = randomNum;
        }
    }
}   

答案 2 :(得分:1)

您可以通过使用while循环和for循环来执行此操作,如下所示。在while循环中,您生成一个数字,然后检查重复。如果它不是重复值,请添加到数组并增加索引值。如果它是重复值,则循环继续时。

 public static void duplication() {
    int max = 100; // max value for range
    int min = 1; // min value for range
    Random rand = new Random();
    int index = 0;   // counter to track 20 numbers
    int[] all = new int[20];

    while (index < 20) {

        boolean duplicate = false;
        int randomNum = rand.nextInt((max - min) + 1) + min;

        for (int i = 0; i < all.length; i++) {
            //if there's a match (duplicate) flag boolean
            if (all[i] == randomNum) {
               duplicate = true;
               break;
            }
        }

        if (!duplicate) {
            all[index++] = randomNum;
        }
    }
}