indexOf()
如何知道如何在传递给ArrayList
的对象的IndexOf()
中获取索引?就像,如果它是String
,那么我得到它,可以比较一个字符串,但它如何识别ArrayList
中的特定对象?这是否与获取对象hashCode()
?
public class QuoteService {
public static List<Quote> quotes = new ArrayList<Quote>();
public Quote addQuote(Integer id, String author, String message) throws Exception {
//Check for already exists
int index = quotes.indexOf(new Quote(id));
if (index != -1) throw new Exception("Quote Record already exists");
Quote q = new Quote(id, author, message);
quotes.add(q);
return q;
}
}
代码取自here。