Java 如何从数组中获取多个随机值?

Java 如何从数组中获取多个随机值?,java,android,arrays,random,Java,Android,Arrays,Random,我正在使用java开发我的第一个android项目 我有一节问题课 public class Question { private int id; private String questionText; private String answer; private TextView textQuestion; public Question(int id, String questionText, String answer,TextView text

我正在使用java开发我的第一个android项目

我有一节问题课

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;
    }
}
我将使用该类创建数组并存储一些问题和答案。但我的问题是,例如,对于id=0,它将是字母“A”,我希望字母“A”至少有5个问题

如果我这样做,我只能为id=0和字母“A”存储一个问题。我该怎么办

编辑:对字母“A”说“我希望至少有5个问题”。我的意思是,有些问题的答案将以“A”或B、C等开头。例如,5个问题的答案以“A”开头。其他每个字母都是一样的。我可以使用该类为每个id和字母创建问题和答案,但我需要一个id有多个问题。 喜欢


你可以这样做

List<Question> list = new ArrayList<>();
for(Question q : questions) {
    String s = q.answer;
    if(s.charAt(0) == 'a') {
        list.add(q);
    }
}
List List=new ArrayList();
关于(问题q:问题){
字符串s=q.答案;
如果(s.charAt(0)=‘a’){
列表。添加(q);
}
}
接下来使用
Random
类,这里是一个示例


我不确定,您希望得到什么效果。是否要填充问题数组?如果是,请对(int i=0;i“5个字母“A”的问题”使用如下循环
“这是什么意思?这些问题与问题标题“如何从数组中获取多个随机值”有何关系?”您在问题正文中没有再次提到“随机”。抱歉,解释不正确。有些问题的答案将以“A”开头或B、C等。例5答案以“A”开头的问题。其他字母相同。textView用于不同的内容,与问题无关。我可以使用此类为每个id和字母创建问题和答案,但我需要为一个id创建多个问题。是否要在
id=0
中保存五个问题?是的,类似于此。每个lette至少有五个问题r(id)。
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);
List<Question> list = new ArrayList<>();
for(Question q : questions) {
    String s = q.answer;
    if(s.charAt(0) == 'a') {
        list.add(q);
    }
}