Java 填充2D数组时如何生成唯一字符?
当我运行代码输出网格时,如何使两个数字或字母不相同?当我当前运行此代码时,我可以得到3xl或2x6,如何使它们只出现一次Java 填充2D数组时如何生成唯一字符?,java,random,multidimensional-array,char,printf,Java,Random,Multidimensional Array,Char,Printf,当我运行代码输出网格时,如何使两个数字或字母不相同?当我当前运行此代码时,我可以得到3xl或2x6,如何使它们只出现一次 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 < usedNumber
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;
}
}
这应该比验证每个随机选择要快得多: 将有效字符存储到数组中
char[] valid = validChars.toCharArray();
)
遍历矩阵中的位置,按照元素在无序数组中的显示顺序存储元素
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++];
}
}
这应该比验证每个随机选择要快得多: 将有效字符存储到数组中
char[] valid = validChars.toCharArray();
)
遍历矩阵中的位置,按照元素在无序数组中的显示顺序存储元素
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++];
}
}
如果旧随机数在地图中,则使用集合并生成新随机数: 伪代码:
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
}
如果旧随机数在地图中,则使用集合并生成新随机数: 伪代码:
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
}
这是如何防止rng生成相同的字符以及在矩阵中结束的相同字符的?@XapaJlaMnu有效数组包含validChars中的36个唯一字符。当你洗牌时,36个角色只会改变位置。所以你只需要把它们放到矩阵中。没有重复的问题需要担心。@XapaJIaMnu你读过答案了吗?您在代码中看到在哪里使用了随机数生成器?如果你洗牌一副36张不同的牌,并把它们摆在一张桌子上,你会重复吗?以上代码就是这么做的。@JBNizet我刚刚才意识到这个解决方案在做什么。我的错。这是如何阻止rng生成相同的字符以及在矩阵中结束的相同字符的?@XapaJlaMnu有效数组包含validChars中的36个唯一字符。当你洗牌时,36个角色只会改变位置。所以你只需要把它们放到矩阵中。没有重复的问题需要担心。@XapaJIaMnu你读过答案了吗?您在代码中看到在哪里使用了随机数生成器?如果你洗牌一副36张不同的牌,并把它们摆在一张桌子上,你会重复吗?以上代码就是这么做的。@JBNizet我刚刚才意识到这个解决方案在做什么。我的错。你所说的集合到底是什么意思?那就是操纵哈希集或树集的方法?抱歉,或者类才刚刚开始,你所说的集合到底是什么意思?那么,操作HashSet或TreeSet的方法是什么呢?抱歉,或者刚开始上课