ListSoucre学习
1.Collection接口
Collection(Interface)Collection接口扩展了Iterable接口,实现了Iterable接口的那些类可以拥有增强for循环
1 public static<AnyType> void print(Collection<AnyType> coll){ 2 for(AnyType item:coll) 3 System.out.println(item); 4 }
2.Iterator接口
实现Iterable接口的集合必须实现提供一个成为iterator的方法,该方法返回一个Iterator的类型对象
1 public interface Iterator<AnyType> 2 { 3 boolean hashNext(); 4 AnType next(); 5 void remove();//可以删除最新返回的项,删除后我们不能再次remove,必须先调用next 6 }
Iterator接口的思想是:通过iterator方法,每个集合均可创建并返回给用户一个实现Iterator的对象
注意:当对正在迭代的集合进行结构上面的改变(删除和添加),那么迭代器将不太合法,会抛出异常,所以也就只有在立即使用一个迭代器的时候才会去获取
3.List接口
List接口
1 public interface List<AnyType> extends Collection<AnyType> 2 { 3 AnyType get(int idx); 4 AnyType set(int idx,AnyType nexVal); 5 void add(int idx,AnyType x); 6 void remove(int idx); 7 ListIterator<AnyType> listIterator(int pos); 8 }
4.ListIterator接口
1 public interface ListIterator<Anytype> extends Iterator<Anytype> 2 { 3 boolean hasPrevious(); 4 Anytype previous(); 5 void add(Anytype x); 6 void set(Anytype newVal); 7 }
5.ArrayList的实现
1 import java.util.Iterator; 2 3 public class MyArrayList<AnyType> implements Iterable { 4 private static final int DEFAULT_CAPACITY=10;//扩展大小 5 private int theSize;//当前数组的大小 6 private AnyType[] theItems; //内部实现 7 8 public MyArrayList() { 9 clear(); 10 } 11 public boolean isEmpty(){ 12 return theSize==0; 13 } 14 public void trimToSize(){ 15 ensureCapacity(size()); 16 } 17 public int size(){ 18 return theSize; 19 } 20 public void clear(){ 21 theSize=0; 22 ensureCapacity(DEFAULT_CAPACITY); 23 } 24 25 public AnyType get(int idx){ 26 if(idx<0||idx>=size()) 27 throw new ArrayIndexOutOfBoundsException(); 28 return theItems[idx]; 29 } 30 public AnyType set(int idx,AnyType newVal){ 31 if(idx<0 || idx>=size()) 32 throw new ArrayIndexOutOfBoundsException(); 33 AnyType old=theItems[idx]; 34 theItems[idx]=newVal; 35 return old; 36 } 37 38 public void ensureCapacity(int newCapacity){//用来扩充数组大小 39 if(newCapacity<theSize) 40 return ; 41 AnyType[] old=theItems; 42 theItems=(AnyType[]) new Object[newCapacity]; 43 for(int i=0;i<size();i++) 44 theItems[i]=old[i]; 45 } 46 47 public boolean add(AnyType x){ 48 add(size(),x); 49 return true; 50 } 51 52 public void add(int idx,AnyType x){ 53 if(theItems.length==size()) 54 ensureCapacity(size()*2+1); 55 for(int i=theSize;i>idx;i--){ 56 theItems[i]=theItems[i-1]; 57 } 58 theItems[idx]=x; 59 } 60 61 public AnyType remove(int idx){ 62 AnyType removeItem=theItems[idx]; 63 for(int i=idx;i<size();i++){ 64 theItems[i-1]=theItems[i]; 65 } 66 theSize--; 67 return removeItem; 68 } 69 @Override 70 public Iterator iterator() { 71 // TODO Auto-generated method stub 72 return new ArrayListIterator(); 73 } 74 75 private class ArrayListIterator implements Iterator<AnyType>{ 76 private int current=0;//当前迭代的位置 77 @Override 78 public boolean hasNext() { 79 return current<size(); 80 } 81 82 @Override 83 public AnyType next() { 84 return theItems[current++]; 85 } 86 public void remove(){ 87 MyArrayList.this.remove(--current); 88 } 89 90 } 91 }
6.LinkedList类的实现
1 import java.util.ConcurrentModificationException; 2 import java.util.Iterator; 3 import java.util.NoSuchElementException; 4 5 public class MyLinkedList<AnyType> implements Iterable<AnyType> { 6 private int theSize;//当前链表的大小 7 private int modCount=0;//用来统计MyLinedList的操作次数,与内部迭代器不想等则抛出异常 8 private Node<AnyType> beaginMarker;//头节点 9 private Node<AnyType> endMarker;//尾节点 10 11 12 private static class Node<AnyType>{//封装节点的信息 13 public AnyType data; 14 public Node<AnyType> prev; 15 public Node<AnyType> next; 16 public Node(AnyType d,Node<AnyType> p, Node<AnyType> n){ 17 data=d; 18 prev=p; 19 next=n; 20 } 21 } 22 23 public MyLinkedList(){ 24 clear(); 25 } 26 public void clear(){ 27 beaginMarker=new Node<AnyType>(null,null,null); 28 endMarker=new Node<AnyType>(null,beaginMarker,null); 29 beaginMarker.next=endMarker; 30 31 theSize=0; 32 modCount++; 33 } 34 public boolean isEmpty(){ 35 return theSize==0; 36 } 37 public int size(){ 38 return theSize; 39 } 40 public boolean add(AnyType x){ 41 add(size(),x); 42 return true; 43 } 44 public boolean add(int idx,AnyType x){ 45 addBefore(getNode(idx),x); 46 return true; 47 } 48 public AnyType get(int idx){ 49 return getNode(idx).data; 50 } 51 52 public AnyType set(int idx,AnyType newVal){ 53 Node<AnyType> p=getNode(idx); 54 AnyType oldVal=p.data; 55 p.data=newVal; 56 return oldVal; 57 } 58 public AnyType remove(int idx){ 59 return remove(getNode(idx)); 60 } 61 private AnyType remove(Node<AnyType> p){ 62 p.next.prev=p.prev; 63 p.prev.next=p.next; 64 theSize--; 65 modCount++; 66 67 return p.data; 68 } 69 private void addBefore(Node<AnyType> p,AnyType x){//把x增加到p节点之前 70 Node<AnyType> newNode=new Node<AnyType>(x,p.prev,p); 71 newNode.prev.next=newNode; 72 p.prev=newNode; 73 theSize++; 74 modCount++; 75 } 76 private Node<AnyType> getNode(int idx){ 77 Node<AnyType> p ; 78 if(idx<0 || idx>0) 79 throw new IndexOutOfBoundsException(); 80 if(idx<size()/2){//从头节点向后搜索p 81 p=beaginMarker.next; 82 for(int i=0;i<idx;i++) 83 p=p.next; 84 }else{//从尾节点向前搜索p 85 p=endMarker.prev; 86 for(int i=size();i>idx;i--) 87 p=p.prev; 88 } 89 return p; 90 } 91 private class LinkedListIterator implements Iterator<AnyType>{ 92 private Node<AnyType> current=beaginMarker.next;//当前节点 93 private int expectModCount=modCount; 94 private boolean okToRemove=false; 95 //如果next已经被执行而没有其后的remove,那么将其设置成true 96 @Override 97 public boolean hasNext() { 98 return current!=endMarker; 99 } 100 101 @Override 102 public AnyType next() { 103 if(modCount!=expectModCount) 104 throw new ConcurrentModificationException(); 105 if(!hasNext()) 106 throw new NoSuchElementException(); 107 AnyType nextItem=current.data; 108 current=current.next; 109 okToRemove=true; 110 return nextItem; 111 } 112 113 public void remove(){ 114 if(modCount!=expectModCount) 115 throw new ConcurrentModificationException(); 116 if(!okToRemove)//避免修改List的结构 117 throw new IllegalStateException(); 118 MyLinkedList.this.remove(current.prev); 119 okToRemove=false; 120 expectModCount++; 121 } 122 } 123 @Override 124 public Iterator<AnyType> iterator() { 125 // TODO Auto-generated method stub 126 return null; 127 } 128 129 }

浙公网安备 33010602011771号