1 22 23 39 40 public class DirectIntArray 41 { 42 43 46 protected int countPresent; 47 48 51 protected int maximumGrowth; 52 53 56 protected int[] baseArray; 57 58 64 65 public DirectIntArray(int size, int growth) { 66 maximumGrowth = growth; 67 baseArray = new int[size]; 68 } 69 70 75 76 public DirectIntArray(int size) { 77 this(size, Integer.MAX_VALUE); 78 } 79 80 85 86 public DirectIntArray(DirectIntArray base) { 87 this(base.baseArray.length, base.maximumGrowth); 88 System.arraycopy(base.baseArray, 0, baseArray, 0, baseArray.length); 89 countPresent = base.countPresent; 90 } 91 92 103 104 protected void growArray(int required) { 105 int size = Math.max(required, 106 baseArray.length + Math.min(baseArray.length, maximumGrowth)); 107 int[] grown = new int[size]; 108 System.arraycopy(baseArray, 0, grown, 0, baseArray.length); 109 baseArray = grown; 110 } 111 112 118 119 public final int add(int value) { 120 int index = countPresent++; 121 if (countPresent > baseArray.length) { 122 growArray(countPresent); 123 } 124 baseArray[index] = value; 125 return index; 126 } 127 128 134 135 public void add(int index, int value) { 136 if (index >= 0 && index <= countPresent) { 137 if (++countPresent > baseArray.length) { 138 growArray(countPresent); 139 } 140 if (index < countPresent) { 141 System.arraycopy(baseArray, index, baseArray, index + 1, 142 countPresent - index - 1); 143 } 144 baseArray[index] = value; 145 } else { 146 throw new ArrayIndexOutOfBoundsException ("Invalid index value"); 147 } 148 } 149 150 156 157 public void remove(int index) { 158 if (index >= 0 && index < countPresent) { 159 if (index < --countPresent){ 160 System.arraycopy(baseArray, index + 1, baseArray, index, 161 countPresent - index); 162 baseArray[countPresent] = 0; 163 } 164 } else { 165 throw new ArrayIndexOutOfBoundsException ("Invalid index value"); 166 } 167 } 168 169 175 176 public final void ensureCapacity(int min) { 177 if (min > baseArray.length) { 178 growArray(min); 179 } 180 } 181 182 185 186 public final void clear() { 187 countPresent = 0; 188 } 189 190 195 196 public final int size() { 197 return countPresent; 198 } 199 200 207 208 public void setSize(int count) { 209 if (count > baseArray.length) { 210 growArray(count); 211 } else if (count < countPresent) { 212 for (int i = count; i < countPresent; i++) { 213 baseArray[i] = 0; 214 } 215 } 216 countPresent = count; 217 } 218 219 225 226 public final int get(int index) { 227 if (index < countPresent) { 228 return baseArray[index]; 229 } else { 230 throw new ArrayIndexOutOfBoundsException ("Invalid index value"); 231 } 232 } 233 234 240 241 public final void set(int index, int value) { 242 if (index < countPresent) { 243 baseArray[index] = value; 244 } else { 245 throw new ArrayIndexOutOfBoundsException ("Invalid index value"); 246 } 247 } 248 249 255 256 public int[] toArray() { 257 int[] copy = new int[countPresent]; 258 System.arraycopy(baseArray, 0, copy, 0, countPresent); 259 return copy; 260 } 261 262 267 268 public Object clone() { 269 return new DirectIntArray(this); 270 } 271 } 272 | Popular Tags |