我有一个包含对象数组的数组列表。该对象有2个值。一个是问题,另一个是答案。对于n个问题,可能有相同的答案。换句话说,答案级别有重复项。我想从数组列表中绘制5个对象,其答案完全不同。
你能指导我一些技巧吗?
ArrayList<ToughQuiz> animalQuizCollection;
animalQuizCollection = new ArrayList<ToughQuiz>();
for(int i=0; i<animalQuestionCount;i++){
toughQuiz = new ToughQuiz(animalQuestion[i], animalAnswer[i]);
animalQuizCollection.add(toughQuiz);
}
答案 0 :(得分:0)
创建循环以验证答案不在您的ArrayList
中ArrayList<ToughQuiz> animalQuizCollection;
boolean find=false;
animalQuizCollection = new ArrayList<ToughQuiz>();
for(int i=0; i<animalQuestionCount;i++){
for(int j=0; j<animalQuizCollection ; j++) {
if(animalAnswer[i].equals( animalQuizCollection.get(j).getAnswer() ) {
find = true;
break;
}
else
find = false;
}
if(!find) {
toughQuiz = new ToughQuiz(animalQuestion[i], animalAnswer[i]);
animalQuizCollection.add(toughQuiz);
}
}
答案 1 :(得分:0)
Map<AminalAnswer, AnimalQuestion> animalQuizCollection;
animalQuizCollection = new HashMap<>(); //diamond operator for java 7 only.
int randomIndex;
Random random = new Random();
while(animalQuizCollection.size() < 5){
randomIndex = random.nextInt(animalAnswer.length);
animalQuizCollection.put(animalAnswer[randomIndex], animalQuestion[randomIndex]);
}
for(AnimalAnswer answer : animalQuizCollection.keySet()) {
// you will have 5 unique answers here and animalQuizCollection.get(answer)
// will get you the question that corresponds to it
}
这是一种快速而肮脏的方法,可以提高效率,特别是使用randoms。然而,它基本上会随机选择答案和问题,并将它们添加到地图中,其中答案是关键,直到地图获得5个条目。
我没有对此进行过测试,因此可能存在错误,但概念似乎很稳固。
答案 2 :(得分:0)
制作一个Comparator来比较WhileQuestion
public class ToughQuizAnswerComparator implements Comparator<ToughQuiz> {
@Override
public int compare(ToughQuiz o1, ToughQuiz o2) {
return o1.getAnswer().compareTo(o2.getAnswer());
}
}
循环通过ToughQuiz:
for (ToughQuiz q : toughQuizs)
创建一个新列表,当你循环第一个列表时,如果该元素尚不存在,则添加到第一个列表,使用Collections类进行检查。
答案 3 :(得分:0)
在equals
中实施ToughQuiz
方法,它为同一答案返回相同的值,即使用答案进行计算。
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof ToughQuiz))
return false;
ToughQuiz other= (ToughQuiz) o;
if (this.answer.equals(other.answer))
return true;
return false;
}
然后,你会得到五个这样的评估:
Random random= new Random();
List<ToughQuiz> result= new ArrayList<>();
for (int i=0; i<5; i++) {
//loop infinitely to find the unique answer
for (;;) {
ToughQuiz quiz = animalQuizCollection.get(random.nextInt(animalQuizCollection.size());
//because you overriden equals this will produce true if there is already an element inside with the same hashCode
if (!result.contains(quiz)) {
result.add(quiz);
break; //break the inner loop
}
}
}
答案 4 :(得分:0)
试试这个:
Set<Answer> answersSet = new HashSet<Answer>();
List<ToughQuiz> differentAnswers = new ArrayList<ToughQuiz>();
for(ToughQuiz quiz : animalQuizCollection){
if(answersSet.add(quiz.getAnswer())){
differentAnswers.add(quiz);
}
if(differentAnswers.size() >= 5) break;
}
在您的情况下, Answer
可能只是String
。
答案 5 :(得分:0)
地图会为您跟踪相应的问题并具有唯一的密钥:
Map<Answer, ToughQuiz> map = new HashMap<>();
for(ToughQuiz tq : animalQuizCollection)
map.put(tq.getAnswer(), tq);
现在 map 包含所有唯一答案及其相应的答案。