以前背单词表的时候,按照顺序头三个字母背的最多,也记得最多,来来回回的记,所以造成了对以c开头的字母有点糊涂,因此选择这样的接口或者方法进行着重记忆一番
1.list.contain(object o );
------>用来比较list集合中是否有与o一样的元素存在。源码分析
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
transient Object[] elementData;
contain是与给的Object[]数组进行比较;因此比较的数组的具体元素,而不是对象的引用
2.创建自定义类型需要的接口comparable
public interface Comparable<T> {
public int compareTo(T o);
}
T: the type of objects that this object may be compared to : 要比较的对象类型
o: the object to be compared 要比较的对象
需要重写方法compare To()