KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > collection > IntArrayList


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util.collection;
4
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectOutputStream JavaDoc;
7 import java.io.Serializable JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 /**
11  * ArrayList of int primitives.
12  */

13 public class IntArrayList implements Serializable JavaDoc {
14
15     private int[] array = null;
16     private int size = 0;
17
18     public static int INITIAL_CAPACITY = 10;
19
20     /**
21      * Constructs an empty list with an initial capacity.
22      */

23     public IntArrayList() {
24         this(INITIAL_CAPACITY);
25     }
26
27     /**
28      * Constructs an empty list with the specified initial capacity.
29      */

30     public IntArrayList(int initialCapacity) {
31         if (initialCapacity < 0) {
32             throw new IllegalArgumentException JavaDoc("Capacity can't be negative: " + initialCapacity);
33         }
34         array = new int[initialCapacity];
35         size = 0;
36     }
37
38     /**
39      * Constructs a list containing the elements of the specified array.
40      * The list instance has an initial capacity of 110% the size of the specified array.
41      */

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     // ---------------------------------------------------------------- conversion
49

50     /**
51      * Returns an array containing all of the elements in this list in the correct order.
52      */

53     public int[] toArray() {
54         int[] result = new int[size];
55         System.arraycopy(array, 0, result, 0, size);
56         return result;
57     }
58
59     // ---------------------------------------------------------------- methods
60

61     /**
62      * Returns the element at the specified position in this list.
63      */

64     public int get(int index) {
65         checkRange(index);
66         return array[index];
67     }
68
69     /**
70      * Returns the number of elements in this list.
71      */

72     public int size() {
73         return size;
74     }
75
76     /**
77      * Removes the element at the specified position in this list.
78      * Shifts any subsequent elements to the left (subtracts
79      * one from their indices).
80      *
81      * @param index the index of the element to remove
82      * @return the value of the element that was removed
83      * @throws UnsupportedOperationException when this operation is not
84      * supported
85      * @throws IndexOutOfBoundsException if the specified index is out of range
86      */

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     /**
98      * Removes from this list all of the elements whose index is between fromIndex,
99      * inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index).
100      */

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     /**
115      * Replaces the element at the specified position in this list with the specified element.
116      *
117      * @param index the index of the element to change
118      * @param element the value to be stored at the specified position
119      * @return the value previously stored at the specified position
120      */

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     /**
129      * Appends the specified element to the end of this list.
130      */

131     public void add(int element) {
132         ensureCapacity(size + 1);
133         array[size++] = element;
134     }
135
136     /**
137      * Inserts the specified element at the specified position in this list.
138      * Shifts the element currently at that position (if any) and any subsequent
139      * elements to the right (adds one to their indices).
140      *
141      * @param index the index at which to insert the element
142      * @param element the value to insert
143      */

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     /**
154      * Appends all of the elements in the specified array to the end of this list.
155      */

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     /**
168      * Appends all of the elements in the specified array at the specified position in this list.
169      */

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     /**
183      * Removes all of the elements from this list.
184      * The list will be empty after this call returns.
185      */

186     public void clear() {
187         size = 0;
188     }
189
190     // ---------------------------------------------------------------- search
191

192     /**
193      * Returns true if this list contains the specified element.
194      */

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     /**
206      * Searches for the first occurence of the given argument.
207      */

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     /**
218      * Returns the index of the last occurrence of the specified object in this list.
219      */

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     /**
230      * Tests if this list has no elements.
231      */

232     public boolean isEmpty() {
233         return size == 0;
234     }
235
236
237
238     // ---------------------------------------------------------------- capacity
239

240     /**
241      * Increases the capacity of this ArrayList instance, if necessary,
242      * to ensure that it can hold at least the number of elements specified by
243      * the minimum capacity argument.
244      */

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     /**
255      * Trims the capacity of this instance to be the list's current size.
256      * An application can use this operation to minimize the storage of some instance.
257      */

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     // ---------------------------------------------------------------- serializable
267

268     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
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 JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
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     // ---------------------------------------------------------------- privates
285

286     private void checkRange(int index) {
287         if (index < 0 || index >= size) {
288             throw new IndexOutOfBoundsException JavaDoc("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 JavaDoc("Index should be at least 0 and at most " + size + ", found " + index);
295         }
296     }
297
298 }
299
Popular Tags