I am using an ArrayList to save Questions and their answers. The ArrayList is made up of Answer Class which has Question No and an ArrayList of answers_marked as follows:
private class Answer {
private long question_no;
private ArrayList<long> answer;
public boolean equals(Object o) {
if (o instanceof Answer)
if (((Answer)o).question_no == this.question_no)
return true;
return false;
}
}
public ArrayList<Answer> answers = new ArrayList<Answer>();
Now, when the user changes his answer I want to look into the answers arraylist and check if the question_no already exists in the answers. If it does then update the answer value for which answer was changed. I am trying to use contains method to check if the question_no already exists, but it always return false. What am I doing wrong here? Which other data-structure would be best suited for to do this?
I am using answers.contains(new Answer(10,20)) to see if the question_no 10 was already answered.
ArrayList<Long>?contains()works just fine with yourequals()implementation, as you can see here. Your problem must be elsewhere.