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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| package com.clh.test.mylist;
import java.util.*;
public class MyArrayList<E> {
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
Object[] elementData;
private int size;
public MyArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
public MyArrayList(Collection<? extends E> e) { this(); addAll(e); }
public int size() { return size; }
public boolean add(E e) { ensureCapacityInternal(size + 1); elementData[size++] = e; return true; }
private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } if (minCapacity - elementData.length > 0) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } if (newCapacity > (Integer.MAX_VALUE - 8)) { if (minCapacity < 0) { throw new OutOfMemoryError(); } newCapacity = (minCapacity > Integer.MAX_VALUE - 8) ? Integer.MAX_VALUE : Integer.MAX_VALUE - 8; } elementData = Arrays.copyOf(elementData, newCapacity); } }
public E add(int index, E element) { if (index > size || index < 0) { throw new IndexOutOfBoundsException("index不合法+ index:" + index + ",size:" + size); } ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, size - index); return null; }
public void addAll(Collection<? extends E> e) { Object[] a = e.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); System.arraycopy(a,0,elementData,size,numNew); size +=numNew; }
public E get(int index) { rangeCheck(index); return (E) elementData[index]; }
private void rangeCheck(int index) { if (index >= size) { throw new IndexOutOfBoundsException("index不合法!!index:" + index + ",size:" + size); } }
public E set(int index, E element) { rangeCheck(index); E oldValue = (E) elementData[index]; elementData[index] = element; return oldValue; }
public E remove(int index) { rangeCheck(index); E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) { System.arraycopy(elementData, index + 1, elementData, index, numMoved); } elementData[--size] = null; return oldValue; }
public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) { if (elementData[index] == null) { remove(index); return true; } } } else { for (int index = 0; index < size; index++) { if (o.equals(elementData[index])) { remove(index); return true; } } } return false; } }
|