1 3 package jodd.util.collection; 4 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.Serializable ; 8 import java.io.IOException ; 9 10 13 public class IntArrayList implements Serializable { 14 15 private int[] array = null; 16 private int size = 0; 17 18 public static int INITIAL_CAPACITY = 10; 19 20 23 public IntArrayList() { 24 this(INITIAL_CAPACITY); 25 } 26 27 30 public IntArrayList(int initialCapacity) { 31 if (initialCapacity < 0) { 32 throw new IllegalArgumentException ("Capacity can't be negative: " + initialCapacity); 33 } 34 array = new int[initialCapacity]; 35 size = 0; 36 } 37 38 42 public IntArrayList(int[] data) { 43 array = new int[(int) (data.length * 1.1) + 1]; 44 size = data.length; 45 System.arraycopy(data, 0, array, 0, size); 46 } 47 48 50 53 public int[] toArray() { 54 int[] result = new int[size]; 55 System.arraycopy(array, 0, result, 0, size); 56 return result; 57 } 58 59 61 64 public int get(int index) { 65 checkRange(index); 66 return array[index]; 67 } 68 69 72 public int size() { 73 return size; 74 } 75 76 87 public int remove(int index) { 88 checkRange(index); 89 int oldval = array[index]; 90 int numtomove = size - index - 1; 91 if (numtomove > 0) { 92 System.arraycopy(array, index + 1, array, index, numtomove); 93 } 94 size--; 95 return oldval; 96 } 97 101 public void removeRange(int fromIndex, int toIndex) { 102 checkRange(fromIndex); 103 checkRange(toIndex); 104 if (fromIndex >= toIndex) { 105 return; 106 } 107 int numtomove = size - toIndex; 108 if (numtomove > 0) { 109 System.arraycopy(array, toIndex, array, fromIndex, numtomove); 110 } 111 size -= (toIndex - fromIndex); 112 } 113 114 121 public int set(int index, int element) { 122 checkRange(index); 123 int oldval = array[index]; 124 array[index] = element; 125 return oldval; 126 } 127 128 131 public void add(int element) { 132 ensureCapacity(size + 1); 133 array[size++] = element; 134 } 135 136 144 public void add(int index, int element) { 145 checkRangeIncludingEndpoint(index); 146 ensureCapacity(size + 1); 147 int numtomove = size - index; 148 System.arraycopy(array, index, array, index + 1, numtomove); 149 array[index] = element; 150 size++; 151 } 152 153 156 public void addAll(int[] data) { 157 int dataLen = data.length; 158 if (dataLen == 0) { 159 return; 160 } 161 int newcap = size + (int) (dataLen * 1.1) + 1; 162 ensureCapacity(newcap); 163 System.arraycopy(data, 0, array, size, dataLen); 164 size += dataLen; 165 } 166 167 170 public void addAll(int index, int[] data) { 171 int dataLen = data.length; 172 if (dataLen == 0) { 173 return; 174 } 175 int newcap = size + (int) (dataLen * 1.1) + 1; 176 ensureCapacity(newcap); 177 System.arraycopy(array, index, array, index + dataLen, size - index); 178 System.arraycopy(data, 0, array, index, dataLen); 179 size += dataLen; 180 } 181 182 186 public void clear() { 187 size = 0; 188 } 189 190 192 195 public boolean contains(int data) { 196 for (int i = 0; i < size; i++) { 197 if (array[i] == data) { 198 return true; 199 } 200 } 201 return false; 202 } 203 204 205 208 public int indexOf(int data) { 209 for (int i = 0; i < size; i++) { 210 if (array[i] == data) { 211 return i; 212 } 213 } 214 return -1; 215 } 216 217 220 public int lastIndexOf(int data) { 221 for (int i = size - 1; i >= 0; i--) { 222 if (array[i] == data) { 223 return i; 224 } 225 } 226 return -1; 227 } 228 229 232 public boolean isEmpty() { 233 return size == 0; 234 } 235 236 237 238 240 245 public void ensureCapacity(int mincap) { 246 if (mincap > array.length) { 247 int newcap = ((array.length * 3) >> 1) + 1; 248 int[] olddata = array; 249 array = new int[newcap < mincap ? mincap : newcap]; 250 System.arraycopy(olddata, 0, array, 0, size); 251 } 252 } 253 254 258 public void trimToSize() { 259 if (size < array.length) { 260 int[] olddata = array; 261 array = new int[size]; 262 System.arraycopy(olddata, 0, array, 0, size); 263 } 264 } 265 266 268 private void writeObject(ObjectOutputStream out) throws IOException { 269 out.defaultWriteObject(); 270 out.writeInt(array.length); 271 for (int i = 0; i < size; i++) { 272 out.writeInt(array[i]); 273 } 274 } 275 276 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 277 in.defaultReadObject(); 278 array = new int[in.readInt()]; 279 for (int i = 0; i < size; i++) { 280 array[i] = in.readInt(); 281 } 282 } 283 284 286 private void checkRange(int index) { 287 if (index < 0 || index >= size) { 288 throw new IndexOutOfBoundsException ("Index should be at least 0 and less than " + size + ", found " + index); 289 } 290 } 291 292 private void checkRangeIncludingEndpoint(int index) { 293 if (index < 0 || index > size) { 294 throw new IndexOutOfBoundsException ("Index should be at least 0 and at most " + size + ", found " + index); 295 } 296 } 297 298 } 299 | Popular Tags |