Possible Duplicate:
How do I remove repeated elements from ArrayList?可能重复:如何从ArrayList中删除重复的元素?
I want to replace a duplicate number (if any) that i store in an ArrayList. I obtianed the numbers for a Random variable. Here is the class.
我想替换存储在ArrayList中的重复数字(如果有的话)。我认为随机变量的数字。这是班级。
import java.util.*;
public class RandomNumbers {
// instance variables
private ArrayList<Integer> randomNumberList = new ArrayList<Integer>();
private Random randomNums = new Random();
public RandomNumbers(){
// generating and adding random numbers to the list
for (int i=0; i<MemoryGame.totalAnswers; i++)
randomNumberList.add(randomNums.nextInt(32));
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
public ArrayList<Integer> getRandomNumbers(){
return randomNumberList;
}
}
My school book tells me how to add,remove and retrieve a number, but not how to replace one that is duplicated.
我的学校书告诉我如何添加,删除和检索数字,但不知道如何替换重复的数字。
thanks.
4 个解决方案
#1
2
If you are not limited to ArrayList
, use a HashSet<Integer>
instead (or better yet, a LinkedHashSet). HashSet<Integer>
will garantee you not to have duplicated values in the collection, LinkedHashSet<Integer>
will do the same and also preserve the ordering the the items.
如果您不限于ArrayList,请使用HashSet
If you insist on having an ArrayList
, then do this :
如果你坚持使用ArrayList,那么执行以下操作:
public RandomNumbers(){
HashSet<Integer> set = new HashSet<Integer>();
// generating and adding random numbers to the list
//for (int i=0; i<MemoryGame.totalAnswers; i++)
while (set.size()<MemoryGame.totalAnswers)
set.add(randomNums.nextInt(32));
randomNumberList.addAll(set); // dump the set in your ArrayList
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
Also, if MemoryGame.totalAnswers == 32
, then you can speed up this process with a random list instead :
此外,如果MemoryGame.totalAnswers == 32,那么您可以使用随机列表加速此过程:
public RandomNumbers(){
// generating and adding random numbers to the list
for (int i=0; i<MemoryGame.totalAnswers; i++)
randomNumberList.add(i);
Collections.shuffle(randomNumberList);
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
** Update **
**更新**
Since MemoryGame.totalAnswers == 8
, forget the last snippet. I'm leaving it there if anyone would happend to need it. You can skip the use of a Set
entirely by folllowing pst's suggestion :
由于MemoryGame.totalAnswers == 8,请忘记最后一个片段。如果有人愿意,我会把它留在那里。您完全可以通过以下pst的建议跳过Set的使用:
public RandomNumbers(){
// generating and adding random numbers to the list
for (int i=0; i<32; i++)
randomNumberList.add(i);
Collections.shuffle(randomNumberList);
// keep only the first ones we need
randomNumberList.removeRange(MemoryGame.totalAnswers + 1, 32);
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
#2
1
For replacing an element in a position use the set()
method:
要替换位置中的元素,请使用set()方法:
randomNumberList.set(index, element);
Of course, the logic to find out which elements are duplicated in the first place is up to you; if it doesn't make sense to have duplicate numbers for your problem, better use a Set
data structure (for instance: HashSet, or LinkedHashSet if preserving insertion order when iterating is important for you) and keep adding random elements to the set until it has the desired size.
当然,找出哪些元素首先重复的逻辑取决于你;如果你的问题有重复数字是没有意义的,最好使用Set数据结构(例如:HashSet,或者在迭代时保留插入顺序对于你很重要的LinkedHashSet)并继续向集合中添加随机元素,直到它为止具有所需的大小。
#3
1
If the ultimate objective is a random list of distinct numbers, and the range of possible numbers is not much larger than the number of results required, consider a shuffle rather than replacing duplicates.
如果最终目标是不同数字的随机列表,并且可能数字的范围不比所需结果的数量大得多,则考虑改组而不是替换重复数。
#4
0
If all you want is as per your last comment
如果你想要的只是你最后的评论
8 random, non-repeated numbers from 0-31
从0到0的8个随机,非重复数字
You don't need any instance variables, random number generating code, or instance methods.
All you need is just this:
您不需要任何实例变量,随机数生成代码或实例方法。你只需要这样:
public static List<Integer> getRandomNumbers() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 32; i++)
list.add(i);
Collections.shuffle(list);
return list.subList(0, 8);
}
#1
2
If you are not limited to ArrayList
, use a HashSet<Integer>
instead (or better yet, a LinkedHashSet). HashSet<Integer>
will garantee you not to have duplicated values in the collection, LinkedHashSet<Integer>
will do the same and also preserve the ordering the the items.
如果您不限于ArrayList,请使用HashSet
If you insist on having an ArrayList
, then do this :
如果你坚持使用ArrayList,那么执行以下操作:
public RandomNumbers(){
HashSet<Integer> set = new HashSet<Integer>();
// generating and adding random numbers to the list
//for (int i=0; i<MemoryGame.totalAnswers; i++)
while (set.size()<MemoryGame.totalAnswers)
set.add(randomNums.nextInt(32));
randomNumberList.addAll(set); // dump the set in your ArrayList
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
Also, if MemoryGame.totalAnswers == 32
, then you can speed up this process with a random list instead :
此外,如果MemoryGame.totalAnswers == 32,那么您可以使用随机列表加速此过程:
public RandomNumbers(){
// generating and adding random numbers to the list
for (int i=0; i<MemoryGame.totalAnswers; i++)
randomNumberList.add(i);
Collections.shuffle(randomNumberList);
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
** Update **
**更新**
Since MemoryGame.totalAnswers == 8
, forget the last snippet. I'm leaving it there if anyone would happend to need it. You can skip the use of a Set
entirely by folllowing pst's suggestion :
由于MemoryGame.totalAnswers == 8,请忘记最后一个片段。如果有人愿意,我会把它留在那里。您完全可以通过以下pst的建议跳过Set的使用:
public RandomNumbers(){
// generating and adding random numbers to the list
for (int i=0; i<32; i++)
randomNumberList.add(i);
Collections.shuffle(randomNumberList);
// keep only the first ones we need
randomNumberList.removeRange(MemoryGame.totalAnswers + 1, 32);
System.out.println("Numbers in the list: " + randomNumberList);
System.out.println("");
}
#2
1
For replacing an element in a position use the set()
method:
要替换位置中的元素,请使用set()方法:
randomNumberList.set(index, element);
Of course, the logic to find out which elements are duplicated in the first place is up to you; if it doesn't make sense to have duplicate numbers for your problem, better use a Set
data structure (for instance: HashSet, or LinkedHashSet if preserving insertion order when iterating is important for you) and keep adding random elements to the set until it has the desired size.
当然,找出哪些元素首先重复的逻辑取决于你;如果你的问题有重复数字是没有意义的,最好使用Set数据结构(例如:HashSet,或者在迭代时保留插入顺序对于你很重要的LinkedHashSet)并继续向集合中添加随机元素,直到它为止具有所需的大小。
#3
1
If the ultimate objective is a random list of distinct numbers, and the range of possible numbers is not much larger than the number of results required, consider a shuffle rather than replacing duplicates.
如果最终目标是不同数字的随机列表,并且可能数字的范围不比所需结果的数量大得多,则考虑改组而不是替换重复数。
#4
0
If all you want is as per your last comment
如果你想要的只是你最后的评论
8 random, non-repeated numbers from 0-31
从0到0的8个随机,非重复数字
You don't need any instance variables, random number generating code, or instance methods.
All you need is just this:
您不需要任何实例变量,随机数生成代码或实例方法。你只需要这样:
public static List<Integer> getRandomNumbers() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 32; i++)
list.add(i);
Collections.shuffle(list);
return list.subList(0, 8);
}