indexOf()如何知道如何在传递给它的对象的ArrayList中获取索引?

时间:2015-06-03 16:59:55

标签: java

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

0 个答案:

没有答案