【问题标题】:Storing non-duplicated numbers in an array将不重复的数字存储在数组中
【发布时间】:2015-04-01 06:33:24
【问题描述】:

概述: 我的程序必须在一个数组中存储从 1 到 100 范围内的 20 个非重复随机生成的数字。 问题: 如果我在内部 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
    }
}   

【问题讨论】:

  • 注意:if (duplicate=false) 应该是 if(!duplicate)(或者使用 == 代替,但不推荐使用)。
  • 考虑到您只需要 20 个数字这一事实,我建议您使用 HashSet 并在条件 while hashSet.size()&lt;=20 下添加数字
  • @TheLostMind 同意 - Set 绝对是解决这个问题的好容器。
  • @TheLostMind 我需要使用一个数组。也许,有人可以同时使用 HashSet 和数组来提供正确的解决方案,以便为观众提供更多选择。
  • @Alnitak - 减少代码的混乱。 :)(虽然会影响时间和空间的复杂性)

标签: java arrays random boolean


【解决方案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;
        }
    }
}

【讨论】:

    【解决方案2】:

    这将为您提供一个介于 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-100 之间。
    • @Speedo - 编辑了我的答案。 :)
    • 另外,如果顺序很重要,我会使用LinkedHashSet 来保留顺序。 HashSet 可能会导致某些数字出现在其他数字之前。
    【解决方案3】:

    首先你需要在循环之前初始化你的数组,其次,不要在第二个循环中将重复设置为 false,因为你最后的数字可以将其设置为 false,即使之前有重复。最后一个,只有在没有找到重复项时才设置值,如果重复项为真,则减少 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;
            }
        }
    }   
    

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多