1 8 9 package net.sourceforge.chaperon.common; 10 11 17 public class IntegerList implements IntegerCollection 18 { 19 private int capacityIncrement = 100; 20 private int elementCount = 0; 21 private int[] list = new int[10]; 22 private int dummy; 23 24 27 public IntegerList() {} 28 29 36 public int add(int value) 37 { 38 ensureCapacity(elementCount+1); 39 list[elementCount++] = value; 40 return elementCount-1; 41 } 42 43 48 public void add(IntegerCollection collection) 49 { 50 for (int i = 0; i<collection.getCount(); i++) 51 add(collection.get(i)); 52 } 53 54 59 public void add(int[] array) 60 { 61 for (int i = 0; i<array.length; i++) 62 add(array[i]); 63 } 64 65 70 public void remove(int index) 71 { 72 if (index>=elementCount) 73 throw new ArrayIndexOutOfBoundsException (index); 74 75 elementCount--; 76 if (index<elementCount) 77 System.arraycopy(list, index+1, list, index, elementCount-index); 78 79 list[elementCount] = 0; 80 } 81 82 88 public void set(int index, int value) 89 { 90 if (index>=elementCount) 91 throw new ArrayIndexOutOfBoundsException (index); 92 93 list[index] = value; 94 } 95 96 103 public int get(int index) 104 { 105 if (index>=elementCount) 106 throw new ArrayIndexOutOfBoundsException (index); 107 108 return list[index]; 109 } 110 111 116 public int getCount() 117 { 118 return elementCount; 119 } 120 121 128 public int indexOf(int value) 129 { 130 for (int i = 0; i<elementCount; i++) 131 if (list[i]==value) 132 return i; 133 134 return -1; 135 } 136 137 144 public boolean contains(int value) 145 { 146 for (int i = 0; i<elementCount; i++) 147 if (list[i]==value) 148 return true; 149 150 return false; 151 } 152 153 158 public void removeValue(int value) 159 { 160 int index; 161 162 while ((index = indexOf(value))!=-1) 163 remove(index); 164 } 165 166 171 public boolean isEmpty() 172 { 173 return (elementCount<=0); 174 } 175 176 181 public int pop() 182 { 183 if (0>=elementCount) 184 throw new java.util.EmptyStackException (); 185 186 return list[--elementCount]; 187 } 188 189 194 public void push(int value) 195 { 196 add(value); 197 } 198 199 204 public void push(int[] values) 205 { 206 add(values); 207 } 208 209 214 public int peek() 215 { 216 if (0>=elementCount) 217 throw new java.util.EmptyStackException (); 218 219 return list[elementCount-1]; 220 } 221 222 225 public void clear() 226 { 227 elementCount = 0; 228 } 229 230 236 public void swap(int index1, int index2) 237 { 238 dummy = list[index1]; 239 list[index1] = list[index2]; 240 list[index2] = dummy; 241 } 242 243 248 public String toString() 249 { 250 StringBuffer buffer = new StringBuffer (); 251 252 buffer.append("["); 253 for (int i = 0; i<elementCount; i++) 254 { 255 buffer.append(String.valueOf(list[i])); 256 if (i<(elementCount-1)) 257 buffer.append(","); 258 } 259 260 buffer.append("]"); 261 return buffer.toString(); 262 } 263 264 269 private void ensureCapacity(int minCapacity) 270 { 271 if (list.length>=minCapacity) 272 return; 273 274 int newCapacity = list.length+capacityIncrement; 275 276 if (capacityIncrement<=0) 277 newCapacity = list.length*2; 278 279 int[] newArray = new int[Math.max(newCapacity, minCapacity)]; 280 281 System.arraycopy(list, 0, newArray, 0, list.length); 282 list = newArray; 283 } 284 } 285 | Popular Tags |