I've created an ArrayList for integers which I would like to fill with 200 numbers. Each number can be within a range between 0 and 1023.
我已经为整数创建了一个ArrayList,我想填充200个数字。每个数字可以在0到1023之间的范围内。
Therefore I've written this code:
因此我写了这段代码:
Random rand = new Random();
ArrayList<Integer> values = new ArrayList<Integer>();
int START_AMOUNT = 200;
for(int i = 0; i < START_AMOUNT;
values.add(rand.nextInt(1024));
}
As You might see, the for-loop will add 200 random numbers to the "values" ArrayList, from 0 to 1023. Now my problem is that I want the Array to have only unique numbers. How can I tell the Random class not to generate any numbers that already are existent in the ArrayList?
正如您可能看到的,for循环将向“values”ArrayList添加200个随机数,从0到1023.现在我的问题是我希望Array只有唯一的数字。如何告诉Random类不要生成ArrayList中已存在的任何数字?
4 个解决方案
#1
4
What I'd do is creating an array of 1023 int composed by 1,2,3,...,1023. Then you shuffle it, and you take only the 200 first terms :
我要做的是创建一个由1,2,3,...,1023组成的1023 int数组。然后你将它洗牌,你只需要200个第一项:
List<Integer> ints = new ArrayList<Integer>();
for(int i = 1; i <= 1023; i++)
{
ints.add(i);
}
Collections.shuffle(ints);
EDIT as suggested by @Bohemian♦
按照@ Bohemian♦的建议编辑
List<Integer> result = ints.subList(0,200);
#2
2
A Set is a Collection that cannot contain duplicate elements.
It models the mathematical set abstraction.
The Set interface contains only methods inherited from Collection
and adds the restriction that duplicate elements are prohibited.
And therefore,
因此,
public boolean add(E e)
Adds the specified element to this set if it is not already present.
[...]
If this set already contains the element,
the call leaves the set unchanged and returns false.
As such, what I'd do is use a Set, then add those to the list:
因此,我要做的是使用Set,然后将其添加到列表中:
List<Integer> values = new ArrayList<Integer>();
Set<Integer> set = new HashSet<Integer>();
while(set.size() < 200)
{
set.add(rand.nextInt(1024));
}
values.addAll(set);
#3
1
Use a Set:
使用套装:
Random rand = new Random();
Set<Integer> values = new HashSet<Integer>();
final int START_AMOUNT = 200;
while(values.size() < START_AMOUNT) {
values.add(rand.nextInt(1024));
}
List<Integer> uniqueList = new ArrayList<Integer>(values);
System.out.println(uniqueList);
#4
0
You could also check if the ArrayList contains the given random number everytime you want to add one.
您还可以在每次添加一个时检查ArrayList是否包含给定的随机数。
Random rand = new Random();
Integer r;
ArrayList<Integer> values = new ArrayList<Integer>();
int START_AMOUNT = 200;
for(int i = 0; i < START_AMOUNT; i++) {
r = rand.nextInt(1024);
If !values.contains(r) {
values.add(r);
} else {
i--;
}
}
Although i think Kabulan0lak's answer would be more performant if that is important.
虽然我认为Kabulan0lak的答案如果重要的话会更有效率。
#1
4
What I'd do is creating an array of 1023 int composed by 1,2,3,...,1023. Then you shuffle it, and you take only the 200 first terms :
我要做的是创建一个由1,2,3,...,1023组成的1023 int数组。然后你将它洗牌,你只需要200个第一项:
List<Integer> ints = new ArrayList<Integer>();
for(int i = 1; i <= 1023; i++)
{
ints.add(i);
}
Collections.shuffle(ints);
EDIT as suggested by @Bohemian♦
按照@ Bohemian♦的建议编辑
List<Integer> result = ints.subList(0,200);
#2
2
A Set is a Collection that cannot contain duplicate elements.
It models the mathematical set abstraction.
The Set interface contains only methods inherited from Collection
and adds the restriction that duplicate elements are prohibited.
And therefore,
因此,
public boolean add(E e)
Adds the specified element to this set if it is not already present.
[...]
If this set already contains the element,
the call leaves the set unchanged and returns false.
As such, what I'd do is use a Set, then add those to the list:
因此,我要做的是使用Set,然后将其添加到列表中:
List<Integer> values = new ArrayList<Integer>();
Set<Integer> set = new HashSet<Integer>();
while(set.size() < 200)
{
set.add(rand.nextInt(1024));
}
values.addAll(set);
#3
1
Use a Set:
使用套装:
Random rand = new Random();
Set<Integer> values = new HashSet<Integer>();
final int START_AMOUNT = 200;
while(values.size() < START_AMOUNT) {
values.add(rand.nextInt(1024));
}
List<Integer> uniqueList = new ArrayList<Integer>(values);
System.out.println(uniqueList);
#4
0
You could also check if the ArrayList contains the given random number everytime you want to add one.
您还可以在每次添加一个时检查ArrayList是否包含给定的随机数。
Random rand = new Random();
Integer r;
ArrayList<Integer> values = new ArrayList<Integer>();
int START_AMOUNT = 200;
for(int i = 0; i < START_AMOUNT; i++) {
r = rand.nextInt(1024);
If !values.contains(r) {
values.add(r);
} else {
i--;
}
}
Although i think Kabulan0lak's answer would be more performant if that is important.
虽然我认为Kabulan0lak的答案如果重要的话会更有效率。