-1

我有一个包含对象数组的数组列表。该对象有 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);     
    }  
4

6 回答 6

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.

于 2013-06-14T15:14:10.990 回答
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);  
        }   
    } 
于 2013-06-14T15:04:25.310 回答
0

地图将为您跟踪相应的问题并具有唯一键:

Map<Answer, ToughQuiz> map = new HashMap<>();  

for(ToughQuiz tq : animalQuizCollection)
    map.put(tq.getAnswer(), tq);  

现在地图包含所有唯一答案及其相应的测验。

于 2013-06-14T15:32:24.240 回答
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
}

这是一种快速而肮脏的方法,可以提高效率,尤其是随机数。然而,它基本上会随机选择答案和问题并将它们添加到地图中,其中答案是关键,直到地图获得 5 个条目。

我没有对此进行测试,因此可能存在错误,但这个概念似乎很可靠。

于 2013-06-14T15:07:01.787 回答
0

做一个比较器来比较 ThoughQuestion

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 类进行检查。

于 2013-06-14T15:08:02.603 回答
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
        }
    }
}
于 2013-06-14T15:11:55.843 回答