I have a question how to get "position" in ArrayList. I don´t mean position of element. Well, i have java class for ArrayList called QiuzModal.java like this:
public class QuizModal {
private String question;
private String option1;
private String option2;
private String option3;
private String option4;
private String answer;
private String typingAnswer;
public QuizModal(String question, String option1, String option2, String option3, String option4, String answer, String typingAnswer) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
this.answer = answer;
this.typingAnswer = typingAnswer;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption4() {
return option4;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getTypingAnswer() {
return typingAnswer;
}
public void setTypingAnswer(String typingAnswer) {
this.typingAnswer = typingAnswer;
}
}
and I am adding questions to the ArrayList this way:
quizModalArrayList.add(new QuizModal("Which color is red ?","cyklamen","white","red","purple","red", null));
quizModalArrayList.add(new QuizModal("How many days does one year have ?", null, null, null, null, "365","365"));
So, I don´t need to find out on which position is for example question about days in a year. I need to find out position of option2 in added question And also to find out if is there any way to set something to that position (for example NULL) if the question doesn´t have options.
CodePudding user response:
Instead of this:
private String option1;
private String option2;
private String option3;
private String option4;
You may want this instead:
private List<String> options;
That way, you would be able to find which option is where and have it more flexible.