??????????д??????????????????????д?????ò?????:
package com.wang.list;
import java.util.Iterator;
public class TestMyArrayList {
public static void main(String[] args) {
MyArrayList<String> list=new MyArrayList<>();
list.add("hello");
list.add("world");
list.add("I");
list.add("am");
list.add("a");
list.add("list");
System.out.println("List?????????:"+list.size());
System.out.println("list?????????????:"+list.get(1));
//????5?????"a"???滻?"the".
list.set(4?? "the");
System.out.println("?滻???????????:"+list.get(4));
//???iterator???????List
Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
??????????:
????List?????????:6
????list?????????????:world
?????滻???????????:the
????hello
????world
????I
????am
????the
????list
???????LinkedList
????LinkedList????????????????????????????????????????????3????.
????1.MyLinkedList????.
????2.Node????????????????????????????????????????????????????????????????.
????3.LinkedListIterator????????????????Iterator??????next().hannext().remove()????.
????MyLinkedList:
package com.wang.list;
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<T> {
//??????????
private int theSize;
//????????????
//?????:???????????????????????ж???????????????????????????????????????????????
private int modCount=0;
//????????????????β???
private Node<T> begin;
private Node<T> end;
public MyLinkedList(){
clear();
}
//???????????
public void clear(){
begin=new Node<T>(null?? null?? null);
end=new Node<T>(null?? begin?? null);
begin.next=end;
theSize=0;
modCount++;
}
public int size(){
return theSize;
}
public boolean isEmpty(){
return size()==0;
}
public boolean add(T value){
add(size()??value);
return true;
}
public void add(int index??T value){
//?????????λ?????????????????????????????getNode(index).??????addBefore????
addBefore(getNode(index)??value);
}
//?????????????P???????????value????????
private void  addBefore(Node<T> p??T value){
Node<T> newNode=new Node<T>(value?? p.prev?? p);
newNode.prev.next=newNode;
p.prev=newNode;
theSize++;
modCount++;
}
//??????????????λ??????????
private Node<T> getNode(int index){
Node<T> p;
if(index<0||index>size()){
throw new IndexOutOfBoundsException();
}
if(index<(size()/2)){
p=begin.next;
for(int i=0;i<index;i++){
p=p.next;
}
}else{
p=end;
for(int i=size();i>index;i--){
p=p.prev;
}
}
return p;
}
//???????????????????
public T get(int index){
return getNode(index).data;
}
//?????????????????????value
public T set(int index??T value){
Node<T> p=getNode(index);
T oldData=p.data;
p.data=value;
return oldData;
}
//??????????????????
public T remove(int index){
return remove(getNode(index));
}
private T remove(Node<T> p){
p.next.prev=p.prev;
p.prev.next=p.next;
theSize--;
modCount++;
return p.data;
}
@Override
public Iterator<T> iterator() {
return new LinkedListIteraor();
}
private  class LinkedListIteraor implements Iterator<T>{
//???????????λ?????
private Node<T> current=begin.next;
//??????????????????????????modCount?????????????????????????????????У?
//?????????????????????????
private int exceptedModCount=modCount;
//?ж????????????????
private boolean canMove=false;
@Override
public boolean hasNext() {
return current!=end;
}
@Override
public T next() {
if(exceptedModCount!=modCount){
throw new java.util.ConcurrentModificationException();
}
if(!hasNext()){
throw new java.util.NoSuchElementException();
}
T nextValue=current.data;
current=current.next;
canMove=true;
return nextValue;
}
@Override
public void remove() {
if(exceptedModCount!=modCount){
throw new java.util.ConcurrentModificationException();
}
if(!canMove){
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
canMove=false;
exceptedModCount++;
}
}
private static class Node<T> {
//??????????
public T data;
//????????????
public Node<T> prev;
//?????????????
public Node<T> next;
public Node(T data?? Node<T> prev?? Node<T> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
}
??????????TestMyLinkedList:
package com.wang.list;
import java.util.Iterator;
public class TestMyLinkedList {
public static void main(String[] args) {
MyLinkedList<Integer> list=new MyLinkedList<>();
list.add(1);
list.add(5);
list.add(7);
list.add(6);
list.add(4);
list.add(2);
list.add(3);
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
list.set(2?? 8);
list.remove(0);
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
}
}
??????????:
????1 5 7 6 4 2 3
????5 8 6 4 2 3
???????????????????????????????ConcurrentModificationException:
???????????????????????????????????????????????????????????????????????????????????????ConcurrentModificationException()???????????????????(??????????????LinkList???):
????for(Integer i:list){
????if(i==6){
????list.remove(6);
????}
????}
???????:?????沢?????????remove()?????????remove()????????????????????????д?????????????????λ??????????????.???????????????java????LinkedList.
????????????????????????????for????????????iterator?????????????б????????????????У??????????????????exceptedModCount?????modCount????????.???????ConcurrentModificationException????.
??????????????????????????iterator()?????????????????????????????????е?remove()??????????е?remove()??????????????????????exceptedModCount++?????????????????????:
????Iterator<Integer> it = list.iterator();
????while(it.hasNext()){
????if(it.next()==6){
????it.remove();
????}
????}
?????:?????????????????ο???JAVA??????????<?????????>??飬?????????????????a??汾?????????£????????????ο????????????????????????>..<