从Java中获取没有重复的数组中的随机元素

时间:2018-05-14 00:06:22

标签: java

我正在创建一个基于java GUI的程序,我有一系列问题。我需要创建一个方法,在单击JButton后获取下一个问题。下一个问题应该随机选择,不应重复先前提出的问题。请参阅下面的代码 - 如何实施getNextQuestion

public class QuestionBank {

    String [] questions;
    int currQuestionIndex;

    public QuestionBank() {
        questions = new String [10]; //increase array size if you will add more questions
        questions[0]= "Which three words describe you best?";
        questions[1]= "Which is your best feature?";
        questions[2]= "Which common saying or phrase describes you?";
        questions[3]= "What’s the best thing that’s happened to you this week?";
        questions[4]= "Who was your role model when you were a child?";
        questions[5]= "Who was your favorite teacher and why?";
        questions[6]= "What was your favorite subject at school?";
        questions[7]= "What did you want to be when you grew up?";
        questions[8]= "If you could have one wish come true what would it be?";
        questions[9]= "Which would you prefer — three wishes over five years or one wish right now?";
        //add more questions
    }

    public String getNextQuestion() {
        //get the next question, randomly from the array and without repeats
    }
}

3 个答案:

答案 0 :(得分:1)

如果您想以随机顺序从集合中获取项目而不重复(在注释中明确说明),您可以先对该集合进行随机播放,然后正常迭代该集合。

在Java中,与数组相比,ArrayList更容易混淆,因此请考虑以下因素:

public class QuestionBank {

    List<String> questions;
    int currQuestionIndex;

    public QuestionBank() {
        questions = new ArrayList<>(); // no size needed
        questions.add("Which three words describe you best?");
        questions.add("Which is your best feature?");
        // add more questions
        currQuestionIndex = 0; // it's 0 by default but this makes it explicit
        Collections.shuffle(questions); // shuffle all questions
    }

    public String getNextQuestion() {
        // get the next question, randomly from the list and without repeats
        if (currQuestionIndex >= questions.size()) {
            return null; // no more questions!
        }
        String nextQuestion = questions.get(currQuestionIndex);
        currQuestionIndex++; // move forward in the shuffled list
        return nextQuestion;
    }
}

如果您必须使用数组而不是List,请参阅this question on shuffling an array。策略的其余部分保持不变。

答案 1 :(得分:1)

如何使用Queue

import java.util.*;

public class QuestionBank {

    static Queue<String> questions;

    public QuestionBank() {
        List<String> tmpQuestions = new ArrayList<>(); // no size needed
        tmpQuestions.add("Which three words describe you best?");
        tmpQuestions.add("Which is your best feature?");
        tmpQuestions.add("Which common saying or phrase describes you?");
        tmpQuestions.add( "What’s the best thing that’s happened to you this week?");
        tmpQuestions.add("Who was your role model when you were a child?");
        tmpQuestions.add("Who was your favorite teacher and why?");
        tmpQuestions.add("What was your favorite subject at school?");
        tmpQuestions.add("What did you want to be when you grew up?");
        tmpQuestions.add("If you could have one wish come true what would it be?");
        tmpQuestions.add( "Which would you prefer — three wishes over five years or one wish right now?");
        Collections.shuffle(tmpQuestions, new Random()); // shuffle all questions

使用pkpndfollowing reason

建议的ArrayDeque
        questions = new ArrayDeque<>(tmpQuestions); 
    }
    public String getNextQuestion() {
        // get the next question, from the randomly populated queue
        if (questions.isEmpty()) {
            return null; // no more questions!
        }
        return questions.remove();
    }

    public static void main(String[] args) {
        QuestionBank qb = new QuestionBank();
        String question;
        while((question = qb.getNextQuestion()) != null){
            System.out.println(question);
        }
    }
}

输出结果为:

    // 1st execution
Who was your role model when you were a child?
What?s the best thing that?s happened to you this week?
Which common saying or phrase describes you?
Which is your best feature?
Which three words describe you best?
What was your favorite subject at school?
Which would you prefer ? three wishes over five years or one wish right now?
If you could have one wish come true what would it be?
Who was your favorite teacher and why?
What did you want to be when you grew up?

//2nd execution
Which common saying or phrase describes you?
What did you want to be when you grew up?
What?s the best thing that?s happened to you this week?
Which three words describe you best?
What was your favorite subject at school?
Which would you prefer ? three wishes over five years or one wish right now?
Who was your role model when you were a child?
Which is your best feature?
Who was your favorite teacher and why?
If you could have one wish come true what would it be?

//3rd execution and so on ....
What?s the best thing that?s happened to you this week?
Which common saying or phrase describes you?
Which is your best feature?
What was your favorite subject at school?
Which would you prefer ? three wishes over five years or one wish right now?
If you could have one wish come true what would it be?
What did you want to be when you grew up?
Which three words describe you best?
Who was your role model when you were a child?
Who was your favorite teacher and why?

答案 2 :(得分:0)

如果您想使用int currQuestionIndex访问数组中的下一个问题,请确保在构造函数中初始化它!

public QuestionBank() {
    // Initialise and define 'questions' String array here ...
    this.currQuestionIndex = 0;
}

假设您只想访问questions数组中的当前String,然后在下次调用该方法时前进到下一个问题:

public String getNextQuestion() {
    // Wrap around when we overshoot the array
    if (currQuestionIndex > questions.length)
        currQuestionIndex = 0;

    currQuestionIndex++;
    return questions[currQuestionIndex-1];
}

也可以选择随机问题,确保import java.util.Random

public String getRandomQuestion() {
    Random rand = new Random();
    return questions[rand.nextInt(questions.length)];
}