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?

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.