How do I shuffle two arrays in same order in java

前端 未结 6 750
温柔的废话
温柔的废话 2021-01-12 01:40

I\'ve got two arrays of question and answers

String questions[] = {
\"Q1?\",
\"Q2?\",
\"Q3?\"};

String answers[] = {
    \"A1?\",
    \"A2?\",
    \"A3?\"}         


        
6条回答
  •  醉梦人生
    2021-01-12 02:14

    Creating a class for holding both the question and answer together would be an easier and more OO solution:

    class QuestionAnswerPair {
        private final String question;
        private final String answer;
    
        public QuestionAnswerPair(String question, String answer) {
            this.question = question;
            this.answer = answer;
        }
    }
    

    And then:

    QuestionAnswerPair[] questions = new QuestionAnswerPair[] {
        // Put questions here
    };
    
    Collections.shuffle(Arrays.asList(questions));
    

提交回复
热议问题