Thursday, January 2, 2014

How to use a "contains(object)" method in ArrayList?




First of all, I am not a native English speaker, so.... understand my poor writing plz :)



I am making a Grade Calculator myself.



I just found on the Internet that "contains" method easily works when you using ArrayList<String> or ArrayList<Integer>.



But I have to use my own class... for instance.. ArrayList<Grade> - this class receives 3 valuables in constructor -



"contains" doesn't work in my own class... I already tested.



If I want to find duplication, do I have to use Iterator?




I will show you parts of Grade and DAO class






Java Code:






public Grade(String subjectName, String category, double score) {
this.subjectName = subjectName;
this.score = score;
this.category = category;
}

public int addGrade(Grade tempGrade) {
/*
Iterator<Grade> it = gradeList.iterator();
while(it.hasNext()){
Grade temp = it.next();
}
*/

if (gradeList.contains(tempGrade)) {
return DUPLICATION;
}
gradeList.add(tempGrade);
return SUCCESS;
}







No comments:

Post a Comment