-2

I'm working on my first project for android using java.

I have a questions class ;

public class Question {
    private int id;
    private String questionText;
    private String answer;
    private TextView textQuestion;

    public Question(int id, String questionText, String answer,TextView textQuestion) {
        this.id = id;
        this.questionText = questionText;
        this.answer = answer;
        this.textQuestion= textQuestion;
    }
}

I will create arrays and store some questions and answers using this class.But my problem is for example for id = 0 it will be the letter "A" , and i would like to have at least 5 questions for letter "A".

Question[] questions = new Question[24];
questions[0] = new Question(0, "Question","Answer",textView);

If i do like this i can only store one question for id = 0 and letter "A".What should i do ?

Edit : by saying "i would like to have at least 5 questions for letter "A"." i mean there will be questions where answers will start with "A" or B,C etc.Example 5 questions which has answer that starts with "A".Same for every other letter.I can use this class to create question and answer for every single id and letter , but i need to have multiple questions for one id. Like ;

questions[0] = new Question(0, "Question","answer which starts with a",textView);
questions[1] = new Question(1, "Question","answer which starts with b",textView);
questions[2] = new Question(2, "Question","answer which starts with c",textView);
MePengusta
  • 763
  • 1
  • 8
  • 18
  • I'm not sure, what effect would you like to get. Do you want to fill your Question array? If yes, use an loop like this `for(int i=0;i – grabarz121 Jul 23 '17 at 12:23
  • "5 questions for letter "A"" What does this mean? And how does any of this relate to the question title "How to get multiple random values from an array?" you didn't mention random again in body of your question. – weston Jul 23 '17 at 12:27
  • sorry for bad explanation.there will be questions where answers will start with "A" or B,C etc.Example 5 questions which has answer that starts with "A".Same for every other letter.textView is for something different ,has nothing to do with question.I can use this class to create question and answer for every single id and letter , but i need to have multiple questions for one id. – MePengusta Jul 23 '17 at 12:32
  • You want to save five questions in `id=0` ? – Payam Asefi Jul 23 '17 at 13:57
  • Yeah something like that.At least five questions for every letter(id). – MePengusta Jul 23 '17 at 14:04

1 Answers1

0

You can do something like this

List<Question> list = new ArrayList<>();
for(Question q : questions) {
    String s = q.answer;
    if(s.charAt(0) == 'a') {
        list.add(q);
    }
}

And next use Random class, here is an example.

get random value from ArrayList

grabarz121
  • 616
  • 5
  • 13