0

Hey, so this is killing me, I have a class category, and the equals looks like this:

public boolean equals(String s){
   return s.equals(name); 
}

this is because i have an arrayList of categories, and i want to do:

int i=catList.indexOf("some name");

but it always returns -1... ive never done any .equals method for an arrayList so i have no idea if im doin it right.
help?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by nschessnerd
0

there are several problems with this

first, this is not how you write an equals() method; it needs to take an argument of type Object, in order to override the default equals() method

second, you really don't want to have your class compare equal to strings, because it violates the symmetry property of equality (i.e. the equality doesn't work the other way around); and also the way you wrote it an object of your type does not equal itself, which is bad

0

thanks for the advice on object but it does function how i want it to. in each category object i have its name, and min/max values. basically i will be adding a category to an arrayList. then i will have another list with groups. and in each group i will have a list of the index of the category that applies to the group. so its easier to say groups.get(i).addCategory(categories.indexOf("entered name"));
thanks again

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.