Java AbstractSequentialList

Java AbstractSequentialList

这次源码比较短2333

可以看到,在AbstractSequentialList中实现了AbstractList中的抽象方法get和抛出UnsupportedOperationException的方法set,add,remove.

同时这些方法都通过listIterator()返回的迭代器实现,但是在AbstractSequentialList中的listIterator()是一个抽象方法,并没有实现.

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public abstract class AbstractSequentialList<E> extends AbstractList<E>{
protected AbstractSequentialList{

}

public E get(int index){
try{
return listIterator(index).next();
} catch(NuSuchElementException exc){
throw new IndexOutOfBoundsException("index: "+index);
}
}

public E set(int index, E element){
try{
ListIterator<E> e =listIterator(index);
E oldVal = e.next();
e.set(element);
return oldVal;
} catch(NoSuchElementException exc){
throw new IndexOutOfBoundsException("index : "+index);
}
}

public void add(int index, E element ){
try{
listIterator(index).add(element);
} catch(NoSuchElementException exc){
throw new IndexOutOfBoundsException("index: "+index);
}
}

public E remove(int index){
try{
ListIterator<E> e = listIterator(index);
E outCast = e.next();
e.remove();
return outCast;
} catch(NoSuchElementException exc){
throw new IndexOutOfBoundsException("index: "+index);
}
}

public boolean addAll(int index, Collection<? extends E> c){
try{
boolean modified = false;
ListIterator<E> e1 = listIterator(index);
Iterator<? extends E> e2 = c.Iterator();
while( e2.hasNext()){
e1.add(e2.next());
modified = true;
}
return modified;
} catch(NoSuchElementException exc){
throw new IndexOutOfBoundsException("index : "+index);
}
}

public Iterator<E> iterator(){return listIterator();}

public abstract ListIterator<E> listIterator(int index);
}
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020 Kfdykme
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信