1.層次圖

image.png
2.接口Collection
-
提供集合類增刪改查,遍歷器基本方法接口
image.png
3.AbstractCollection<E>
- 1.實(shí)現(xiàn)父接口isEmpty(), contains(Object o),toArray() , remove(Object o) ,addAll(Collection<? extends E> c) ,removeAll(Collection<?> c) ,retainAll(Collection<?> c),clear() 方法;
2.等待子類重寫實(shí)現(xiàn)方法:Iterator接口方法 (iterator(),hasNext(), next(),remove()),size(),add(E e) - 方法使用
- 如何使用iterator()遍歷集合
public boolean removeAll(Collection<?> c) {
....
Iterator<?> it = iterator();//得到集合的迭代器
while (it.hasNext())//判斷是否有下個元素 {
if (c.contains(it.next()))//得到下個元素 {
.....
}
}
return modified;
}
- 反射創(chuàng)建數(shù)組
public <T> T[] toArray(T[] a) {
// 若傳入數(shù)組洗浴集合大小則反射創(chuàng)建集合大小的數(shù)組
int size = size();
T[] r = a.length >= size ? a :(T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
.....
}

image.png
4.List與Set接口
-
LIst比Colllection增加以下方法
image.png - Set只是繼承了Colllection所有方法
5.java 設(shè)計(jì)思想一:
- 為什么接口Set與抽象類AbstractCollection方法相同,AbstractSet要實(shí)現(xiàn)Set和繼承stractCollection,這樣不是重復(fù)嗎?
- 抽象類體現(xiàn)了數(shù)據(jù)抽象的思想,是實(shí)現(xiàn)多態(tài)的一種機(jī)制。抽象類是對類抽象,而接口是對行為的抽象。抽象類是對整個類整體進(jìn)行抽象,包括屬性、行為,但是接口卻是對類局部(行為)進(jìn)行抽象。 抽象類所跨域的是具有相似特點(diǎn)的類,而接口卻可以跨域不同特點(diǎn)的類。對于抽象類而言,它是自下而上來設(shè)計(jì)的,我們要先知道子類才能抽象出父類,而接口則不同,它根本就不需要知道子類的存在,只需要定義一個規(guī)則即可,至于什么子類、什么時(shí)候怎么實(shí)現(xiàn)它一概不知。
-
簡單來說:AbstractCollection是實(shí)現(xiàn)父子繼承關(guān)系與屬性的,只能有一個。而Set是定義這個類的行為和方法,且定義的方法在類中必須實(shí)現(xiàn)。就像動物是AbstractCollection,老虎是AbstractSet,而吃飯這個行為是Set定義的,與老虎本身屬性無關(guān),因?yàn)槿艘部梢猿燥垺?/p>
image.png
6.AbstractList
- 1.待子類重寫的方法:List接口中的get(int index),set(int index, E element),add(int index, E element) ,remove(int index), size()。注意由于繼承AbstractCollection,父類實(shí)現(xiàn)方法該抽象類已經(jīng)自動繼承。另本類已重寫待子類重寫的父類方法。

image.png
- 2.方法學(xué)習(xí)
- 靜態(tài)內(nèi)部類
使用時(shí)機(jī):只在本類使用,構(gòu)造內(nèi)部類。
- 靜態(tài)內(nèi)部類
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
......
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
//內(nèi)部類方法調(diào)用
public E next() {
checkForComodification();
//正常調(diào)用外部類方法,外部類調(diào)用內(nèi)部類方法,內(nèi)部類設(shè)為靜態(tài),或new內(nèi)部類對象
E next = get(i)
}
....
final void checkForComodification() { }
}
private class ListItr extends Itr implements ListIterator<E> { }
}
- 重寫equals方法與hashCode
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
return (this == o);
}
public int hashCode() {
int hashCode = 1;
for (E e : this){
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
}
return hashCode;
}
- 數(shù)組增刪改查可用方法
//1.要拷貝復(fù)制的原始數(shù)據(jù)
//2.原始數(shù)據(jù)的讀取位置(從原始數(shù)據(jù)哪個位置開始拷貝)
//3.存放要拷貝的原始數(shù)據(jù)的目的地
//4.開始存放的位置()
//5.要讀取的原始數(shù)據(jù)長度(拷貝多長)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
- 迭代器的實(shí)現(xiàn)
每一個迭代器保存cursor,lastRet,expectedModCount三個變量。
cursor初始為0,對于每次調(diào)用next方法就增加一次,表示迭代的當(dāng)前位置。
lastRet:保存了每次跳過的元素的坐標(biāo),用于迭代remove操作;
expectedModCount:記錄這個迭代器對對象進(jìn)行結(jié)構(gòu)性修改的次數(shù)。這樣每次迭代器進(jìn)結(jié)構(gòu)性修改的時(shí)候都將expectedModCount 和modCount進(jìn)行對比,如果兩種相等則說明沒有其他迭代器修改了對象,可以進(jìn)行。如果不相等則說明有迭代進(jìn)行了修改,立刻拋出異常。
7.AbstractSet
- 1.等待子類重寫實(shí)現(xiàn)方法:Iterator接口方法 (iterator(),hasNext(), next(),remove()),size(),contains(Object o),add(E e)
- 2.重寫方法:equels,hashcode,removeAll

image.png


