【问题标题】:How to generate unique chars when populating a 2D array?填充二维数组时如何生成唯一字符?
【发布时间】:2015-06-05 04:28:55
【问题描述】:

如何做到这一点,以便在运行代码时输出网格时,没有两个数字或字母会相同?当我当前运行此代码时,我可以获得 3x“L”或 2x“6”,我该如何让它只出现一次?

package polycipher;

import java.util.ArrayList;

public class Matrix {
private char[][] matrix = new char[6][6];
private int[] usedNumbers = new int[50];

{for(int x = 0; x < usedNumbers.length; x++) usedNumbers[x] = -1;}

private final char[] CIPHER_KEY = {'A','D','F','G','V','X'};
private final String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

public Matrix() {
    int random;
    for(int i = 0; i < CIPHER_KEY.length; i++) {
        for(int j = 0; j < CIPHER_KEY.length; j++) {
            validation: while(true) {
                random = (int)(Math.random()*validChars.length()-1);
                for(int k = 0; k < usedNumbers.length; k++) {
                    if(random == usedNumbers[k]) continue validation;
                    else if(usedNumbers[k]==-1) usedNumbers[k] = random;
                }
                break;
            }
            matrix[i][j] = validChars.split("")[random].charAt(0);  
        }
    }
}

public String toString() {
    String output = "   A D F G V X\n";
    for(int i = 0; i < CIPHER_KEY.length; i++) {
        output += CIPHER_KEY[i] + "  ";
        for(int j = 0; j < CIPHER_KEY.length; j++) {
            output += matrix[i][j] + " ";
        }
        output += "\n";
    }
    return output;
}
}

【问题讨论】:

    标签: java random multidimensional-array char printf


    【解决方案1】:

    这应该比验证每个随机选择要快得多:

    1. 将您的有效字符存储到一个数组中;

      char[] valid = validChars.toCharArray();
      
    2. Shuffle the array;

      shuffle(valid)
      
    3. 遍历矩阵中的位置,按照它们在打乱数组中出现的相同顺序存储元素。

      assert (CIPHER_KEY.length * CIPHER_KEY.length) <= valid.length;
      int k = 0;
      for (int i = 0; i < CIPHER_KEY.length; i++) {
          for (int j = 0; j < CIPHER_KEY.length; j++) {
              matrix[i][j] = valid[k++];
          }
      }
      

    【讨论】:

    • 这如何防止 rng 生成相同的字符和相同的字符在矩阵中结束?
    • @XapaJlaMnu valid 数组包含来自 validChars 的 36 个唯一字符。当你洗牌时,这 36 个字符只会改变位置。因此,您只需要将它们放入矩阵中。无需担心重复。
    • @XapaJIaMnu 你读过答案吗?您在哪里看到代码中使用了随机数生成器?如果你洗一副 36 张不同的牌,然后把它们放在桌子上,你会重复吗?这就是上面代码的作用。
    • @JBNizet 刚才我意识到这个解决方案在做什么。我的错。
    【解决方案2】:

    如果旧随机数在地图中,则使用一个集合并生成一个新随机数: 伪代码:

    Set<Integer> set = new HashSet<Integer>();
    for () {
        int random = (int)(Math.random()*validChars.length()-1);
        //Your code for validation here (move it to a function)
        while (!set.contains(random)){
             int random = (int)(Math.random()*validChars.length()-1);
             //Your code for validation here (move it to a function)
        }
        //If we exit this loop it means the set doesn't contain the number
        set.add(random);
        //Insert your code here
    }
    

    【讨论】:

    • 一组到底是什么意思?
    • 那么只是操作 HashSet 或 TreeSet 的方法吗?抱歉或课程才刚刚开始
    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多