Java 将新数组项与其所有祖先项进行比较
我试图创建一个由唯一的随机整数组成的数组。我构建的方法(如下所示)几乎可以工作。问题在于while循环,它将新的随机条目与其直接祖先进行比较。此条件确保数组中不会同时存在一对匹配值。不幸的是,这种情况不会将新的随机值与迄今为止生成的每个随机值进行比较。我怎样才能做到这一点Java 将新数组项与其所有祖先项进行比较,java,arrays,loops,random,while-loop,Java,Arrays,Loops,Random,While Loop,我试图创建一个由唯一的随机整数组成的数组。我构建的方法(如下所示)几乎可以工作。问题在于while循环,它将新的随机条目与其直接祖先进行比较。此条件确保数组中不会同时存在一对匹配值。不幸的是,这种情况不会将新的随机值与迄今为止生成的每个随机值进行比较。我怎样才能做到这一点 private static Random rand = new Random(); //'dynamicLength' is dependent on another object. With the way I've s
private static Random rand = new Random();
//'dynamicLength' is dependent on another object. With the way I've set up the
//method, the size shouldn't matter.
private static int[] randArr = new int[dynamicLength];
public static int[] randGenArr(int upBound, int lowBound)
{
int newRand;
for (int i1 = 0; i1 < randArr.length; i1++)
{
//new random value
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
//walk through the randArr up to the point we initialized
//last iteration
for (int i2 = 0; i2 <= i1; i2++)
{
//compare the new value to its IMMEDIATE ancestor
while (newRand == randArr[i2])
{
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
}
}
//after validation, initialize randArr
randArr[i1] = newRand;
}
return randArr;
}
private static Random rand=new Random();
//“dynamicLength”依赖于另一个对象。用我设置的方式
//方法,大小应该无关紧要。
私有静态int[]randArr=new int[dynamicLength];
公共静态int[]randGenArr(int上界,int下界)
{
内特纽兰;
for(int i1=0;i1 for(int i2=0;i2在外部for循环中:
// generate a number and check if it is already contained in the array
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
boolean contained = isContained(newRand, randArr, i1);
// if it is contained, repeat
while(contained) {
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
contained = isContained(newRand, randArr, i1);
}
randArr[i1] = newRand;
而是包含的
功能:
private static boolean isContained(int e, int[] array, int upTo) {
for(int i = 0 ; i < upTo ; i++) {
if(array[i] == e) {
return true;
}
}
return false;
}
包含私有静态布尔值(int e,int[]数组,int up){
对于(int i=0;i
在外部for循环中:
// generate a number and check if it is already contained in the array
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
boolean contained = isContained(newRand, randArr, i1);
// if it is contained, repeat
while(contained) {
newRand = rand.nextInt(upBound - lowBound + 1) + lowBound;
contained = isContained(newRand, randArr, i1);
}
randArr[i1] = newRand;
而是包含的
功能:
private static boolean isContained(int e, int[] array, int upTo) {
for(int i = 0 ; i < upTo ; i++) {
if(array[i] == e) {
return true;
}
}
return false;
}
包含私有静态布尔值(int e,int[]数组,int up){
对于(int i=0;iInteger[] randNumbers(int amount, int lower, int upper) {
Set<Integer> result = new HashSet<>();
while(result.size() < amount) { //try until there are enough numbers in result
result.add( /*generate new number*/); //add only unique number to result
}
return result.toArray(new Integer[amount]); //converts Set to array
}
Integer[]随机数(整数金额、整数下限、整数上限){
Set result=new HashSet();
while(result.size()
Set是跟踪唯一项的好工具。您可以使用Set生成随机数,并且只能在结束返回数组中使用
Integer[] randNumbers(int amount, int lower, int upper) {
Set<Integer> result = new HashSet<>();
while(result.size() < amount) { //try until there are enough numbers in result
result.add( /*generate new number*/); //add only unique number to result
}
return result.toArray(new Integer[amount]); //converts Set to array
}
Integer[]随机数(整数金额、整数下限、整数上限){
Set result=new HashSet();
while(result.size()
纳扎里的集合
答案几乎肯定会是您想要的答案,但为了我最初建议的完整性,您可以执行以下操作:
public static int[] randGenArr(int upBound, int lowBound) {
// First we'll put all our possible values into a List
List<Integer> vals = new ArrayList(upBound - lowBound + 1);
for(int i = lowBound; i < upBound; i++) {
vals.add(i);
}
// Now put that list into a random order
Collections.shuffle(vals);
// now grab the first X we want and put them into the array we're setting, or if we could return a List<Integer> we could create a SubList
for(int i = 0; i < ranArr.length; i++) {
ranArr[i] = vals.get(i);
}
}
public static int[]randGenArr(int上界,int下界){
//首先,我们将把所有可能的值放入一个列表中
List VAL=新数组列表(上行链路-下行链路+1);
for(int i=下限;i
纳扎里的集合
答案几乎肯定会是您想要的答案,但为了我最初建议的完整性,您可以执行以下操作:
public static int[] randGenArr(int upBound, int lowBound) {
// First we'll put all our possible values into a List
List<Integer> vals = new ArrayList(upBound - lowBound + 1);
for(int i = lowBound; i < upBound; i++) {
vals.add(i);
}
// Now put that list into a random order
Collections.shuffle(vals);
// now grab the first X we want and put them into the array we're setting, or if we could return a List<Integer> we could create a SubList
for(int i = 0; i < ranArr.length; i++) {
ranArr[i] = vals.get(i);
}
}
public static int[]randGenArr(int上界,int下界){
//首先,我们将把所有可能的值放入一个列表中
List VAL=新数组列表(上行链路-下行链路+1);
for(int i=下限;i
以下是一种不使用集合检查重复条目的方法(可能存在小错误)
使用此选项,您可以检查所有当前数字(准确地说,您可以一直检查到randArr
结束),如果发现一个副本,您可以滚动一个新的随机数字
您现在要做的是在当前程序中实现这一点,因为我不知道如何确保新的随机数不在数组中。下面是一种不使用集合检查重复条目的方法(可能会出现小错误) 使用此选项,您可以检查所有当前数字(准确地说,您可以一直检查到
randArr
结束),如果发现一个副本,您可以滚动一个新的随机数字
你现在要做的就是把这个实现到你当前的程序中,因为我不知道如何确保新的随机数不在数组中。
你要考虑在<代码>上行/>和<代码>下界之间有多少个值?如果你希望使用所有的或几乎所有的值,你可以把它们全部放在一个列表中。shuffle
对列表进行洗牌,这样就不会有重复,但会以随机顺序进行。@EricRenouf我尝试了洗牌,但没有结果,您能否给出明确的回答。也许我使用了错误的洗牌。哦,最多我会对我的范围进行30%的采样。这不是一个简单的解决方案:每个循环将新数字与所有当前数字进行比较,或者s for:每个都不能在数组上工作?@MeikVtune我不确定,但我想你说的是一个增强的for循环。Jean Logeart,见下文,我和他就此进行了一次对话,我们得出结论,值0永远不能以这种方式生成。由于默认情况下数组是用全零初始化的,所以增强的for循环将读取零,永远不允许rand值为0。我将形成一个答案,如果我错了,将其删除,最好是k