|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package org.le; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author yue |
| 8 | + * @time 2017年2月19日 |
| 9 | + */ |
| 10 | +public class ArrayList<E> implements List<E> { |
| 11 | + |
| 12 | + private Object[] elementData; |
| 13 | + |
| 14 | + private int size; |
| 15 | + |
| 16 | + public ArrayList(int initCapcity){ |
| 17 | + if(initCapcity < 0){ |
| 18 | + throw new IllegalArgumentException("initCapcity 必须大于0"); |
| 19 | + } |
| 20 | + elementData = new Object[initCapcity]; |
| 21 | + } |
| 22 | + |
| 23 | + public ArrayList(){ |
| 24 | + elementData = new Object[10]; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void add(Object obj) { |
| 29 | + grow(size + 1); |
| 30 | + elementData[size++] = obj; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void add(int index, Object obj) { |
| 35 | + rangeCheckForAdd(index); |
| 36 | + grow(size + 1); |
| 37 | + System.arraycopy(elementData, index, elementData, index+1, size - index); |
| 38 | + elementData[index] = obj; |
| 39 | + size ++; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void remove(Object obj) { |
| 44 | + if(obj == null){ |
| 45 | + for (int i = 0; i < size; i++) { |
| 46 | + if(elementData[i] == null){ |
| 47 | + fastRemove(i); |
| 48 | + } |
| 49 | + } |
| 50 | + }else{ |
| 51 | + for (int i = 0; i < size; i++) { |
| 52 | + if(obj.equals(elementData[i])){ |
| 53 | + fastRemove(i); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public E remove(int index) { |
| 61 | + rangeCheck(index); |
| 62 | + int movedNum = size - index - 1; |
| 63 | + E oldElement = elementData(index); |
| 64 | + System.arraycopy(elementData, index+1, elementData, index, movedNum); |
| 65 | + elementData[--size] = null; |
| 66 | + return oldElement; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public E get(int index) { |
| 71 | + rangeCheck(index); |
| 72 | + return elementData(index); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public E set(int index, E obj) { |
| 77 | + rangeCheck(index); |
| 78 | + E oldElement = elementData(index); |
| 79 | + elementData[index] = obj; |
| 80 | + return oldElement; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int indexOf(E obj) { |
| 85 | + if(obj == null){ |
| 86 | + for (int i = 0; i < size; i++) { |
| 87 | + if(elementData[i] == null){ |
| 88 | + return i; |
| 89 | + } |
| 90 | + } |
| 91 | + }else{ |
| 92 | + for (int i = 0; i < size; i++) { |
| 93 | + if(obj.equals(elementData[i])){ |
| 94 | + return i; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 数组扩容 |
| 103 | + * @param minCapacity |
| 104 | + */ |
| 105 | + private void grow(int minCapacity) { |
| 106 | + if(minCapacity <= elementData.length){ |
| 107 | + return; |
| 108 | + } |
| 109 | + int oldCapacity = elementData.length; |
| 110 | + int newCapacity = minCapacity + (oldCapacity >> 1); |
| 111 | + if(newCapacity < minCapacity){ |
| 112 | + newCapacity = minCapacity; |
| 113 | + } |
| 114 | + if(minCapacity > Integer.MAX_VALUE){ |
| 115 | + newCapacity = Integer.MAX_VALUE; |
| 116 | + } |
| 117 | + Object[] newArray = new Object[newCapacity]; |
| 118 | + System.arraycopy(elementData, 0, newArray, 0, newCapacity); |
| 119 | + elementData = newArray; |
| 120 | + } |
| 121 | + |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + private E elementData(int index){ |
| 124 | + return (E) elementData[index]; |
| 125 | + } |
| 126 | + |
| 127 | + private void fastRemove(int i) { |
| 128 | + int numMoved = size - i -1; |
| 129 | + if(numMoved > 0){ |
| 130 | + System.arraycopy(elementData, i+1, elementData, i, numMoved); |
| 131 | + } |
| 132 | + elementData[-- size] = null; |
| 133 | + } |
| 134 | + |
| 135 | + private void rangeCheck(int index){ |
| 136 | + if(index >= size || index <0) |
| 137 | + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); |
| 138 | + } |
| 139 | + |
| 140 | + private void rangeCheckForAdd(int index){ |
| 141 | + if(index > size || index <0) |
| 142 | + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); |
| 143 | + } |
| 144 | +} |
0 commit comments