手寫ArrayList
阿新 • • 發佈:2018-04-30
exc pac turn rar ble equal null tor list
ArrayList是Java中最為簡單也是最為常用的容器之一,那麽它的內部構造是什麽呢?其實ArrayList的底層實現就是數組,它的各種方法就是對數組進行的一系列操作,例如對數組元素的修改,刪除,位移,對數組長度的獲取等。我們可以手寫一個ArrayList容器,以助我們更好的理解這個容器:
1 package ArraryList; 2 3 import java.util.Arrays; 4 import java.util.Iterator; 5 6 /** 7 * 深入叠代器的原理-->一個容器可以創建多個叠代器對象 8 * 1.使用了內部類,實現叠代器9 * 2.使用Iterable實現foreach叠代 10 * @author XieHuazhen 11 * 12 */ 13 public class MyArrayList<E> implements Iterable<E>{ //叠代器接口 14 15 //數組存儲值 16 private Object[] elementData; 17 //大小 18 private int size; 19 20 public MyArrayList(int initialCapacity) {21 if(initialCapacity>=0) { 22 elementData = new Object[initialCapacity]; 23 }else { 24 try { 25 throw new MyException(); 26 }catch(MyException e) { 27 e.printInit(); 28 } 29 } 30 }31 32 public MyArrayList() { 33 this(0); 34 } 35 36 @SuppressWarnings("unchecked") 37 private E elementData(int index) { 38 return (E)elementData[index]; 39 } 40 41 private void ensureCapacity() { 42 if(size>=elementData.length) { 43 elementData = Arrays.copyOf(elementData,size*2+1); 44 } 45 } 46 47 private void rangeCheck(int index) { 48 if(index<0||index>=size) { 49 try { 50 throw new MyException(); 51 } catch (MyException e) { 52 e.printIndex(); 53 } 54 } 55 } 56 57 public int size() { 58 return size; 59 } 60 61 public boolean isEmpty() { 62 return size==0; 63 } 64 65 public boolean contains(Object o) { 66 return indexOf(o)>=0; 67 } 68 69 public int indexOf(Object o) { 70 for(int i=0;i<size;++i) { 71 if(elementData[i].equals(o)) 72 return i; 73 } 74 return -1; 75 } 76 77 public E get(int index) { 78 rangeCheck(index); 79 return elementData(index); 80 } 81 82 public E set(int index,E element) { 83 rangeCheck(index); 84 E oldValue = elementData(index); 85 elementData[index] = element; 86 return oldValue; 87 } 88 89 public void add(E element) { 90 ensureCapacity(); 91 elementData[size++] = element; 92 } 93 94 public void add(int index,E element) { 95 rangeCheck(index); 96 ensureCapacity(); 97 System.arraycopy(elementData,index,elementData,index+1,size-index); 98 elementData[index] = element; 99 ++size; 100 } 101 102 public E remove(int index) { 103 rangeCheck(index); 104 E oldValue = elementData(index); 105 System.arraycopy(elementData,index+1,elementData,index,size-index-1); 106 elementData[--size] = null; 107 return oldValue; 108 } 109 110 public boolean remove(E element) { 111 for(int i=0;i<size;++i) { 112 if(elementData[i].equals(element)) { 113 remove(i); 114 return true; 115 } 116 } 117 return false; 118 } 119 120 //實現Iterable<E>接口的方法 121 @Override 122 public Iterator<E> iterator() { 123 return new MyIter(); 124 } 125 126 //內部類 127 private class MyIter implements Iterator<E>{ 128 //計數器-->指針 遊標 129 private int cursor; 130 private int lastRet = -1; 131 132 //判斷是否存在下一個 133 @Override 134 public boolean hasNext() { 135 return cursor!=size; 136 } 137 138 //返回遊標當前位置,並把遊標移到下一位置 139 @SuppressWarnings("unchecked") 140 @Override 141 public E next() { 142 if(!hasNext()) { 143 try { 144 throw new MyException(); 145 } catch (MyException e) { 146 e.printnext(); 147 } 148 } 149 lastRet = cursor; 150 return (E)elementData[cursor++]; 151 } 152 153 //刪除遊標左面元素,執行完next後只能執行一次 154 @Override 155 public void remove() { 156 if(lastRet<0) { 157 try { 158 throw new MyException(); 159 } catch (MyException e) { 160 e.printremove(); 161 } 162 } 163 System.arraycopy(elementData,lastRet+1,elementData,lastRet,size-lastRet-1); 164 cursor = lastRet; 165 lastRet = -1; 166 elementData[--size] = null; 167 } 168 } 169 170 public static void main(String[] args) { 171 MyArrayList<String> list = new MyArrayList<String>(); 172 list.add("aaa"); 173 list.add("bbb"); 174 list.add("ccc"); 175 list.add(2,"123"); 176 list.remove("ccc"); 177 //使用叠代器遍歷 178 for(Iterator<String> itr=list.iterator();itr.hasNext();) { 179 System.out.println(itr.next()); 180 //itr.remove(); 181 } 182 183 System.out.println("---------------------"); 184 185 //使用增強for循環遍歷 186 for(String str:list){ 187 System.out.println(str); 188 } 189 } 190 }
1 package ArraryList; 2 3 public class MyException extends Exception{ 4 5 private static final long serialVersionUID = 1L; 6 7 public MyException() { 8 System.out.print("出現異常:"); 9 } 10 11 public void printIndex() { 12 System.out.println("索引範圍越界!"); 13 System.exit(1); 14 } 15 16 public void printInit(){ 17 System.out.println("請重新初始化"); 18 } 19 20 public void printnext() { 21 System.out.println("遊標越界!"); 22 System.exit(1); 23 } 24 25 public void printremove() { 26 System.out.println("remove()只有在執行完next()後才能且只能執行一次!"); 27 System.exit(1); 28 } 29 }
手寫ArrayList