, backed by an array. All
optional operations adding, removing, and replacing are supported. The
elements can be any objects.
| Method from java.util.ArrayList Detail: |
public boolean add(E object) {
if (lastIndex == array.length) {
growAtEnd(1);
}
array[lastIndex++] = object;
modCount++;
return true;
}
Adds the specified object at the end of this {@code ArrayList}. |
public void add(int location,
E object) {
int size = lastIndex - firstIndex;
if (0 < location && location < size) {
if (firstIndex == 0 && lastIndex == array.length) {
growForInsert(location, 1);
} else if ((location < size / 2 && firstIndex > 0)
|| lastIndex == array.length) {
System.arraycopy(array, firstIndex, array, --firstIndex,
location);
} else {
int index = location + firstIndex;
System.arraycopy(array, index, array, index + 1, size
- location);
lastIndex++;
}
array[location + firstIndex] = object;
} else if (location == 0) {
if (firstIndex == 0) {
growAtFront(1);
}
array[--firstIndex] = object;
} else if (location == size) {
if (lastIndex == array.length) {
growAtEnd(1);
}
array[lastIndex++] = object;
} else {
throw new IndexOutOfBoundsException(
// luni.0A=Index: {0}, Size: {1}
Messages.getString("luni.0A", //$NON-NLS-1$
Integer.valueOf(location),
Integer.valueOf(lastIndex - firstIndex)));
}
modCount++;
}
Inserts the specified object into this {@code ArrayList} at the specified
location. The object is inserted before any previous element at the
specified location. If the location is equal to the size of this
{@code ArrayList}, the object is added at the end. |
public boolean addAll(Collection<? extends E> collection) {
Object[] dumpArray = collection.toArray();
if (dumpArray.length == 0) {
return false;
}
if (dumpArray.length > array.length - lastIndex) {
growAtEnd(dumpArray.length);
}
System.arraycopy(dumpArray, 0, this.array, lastIndex, dumpArray.length);
lastIndex += dumpArray.length;
modCount++;
return true;
}
Adds the objects in the specified collection to this {@code ArrayList}. |
public boolean addAll(int location,
Collection<? extends E> collection) {
int size = lastIndex - firstIndex;
if (location < 0 || location > size) {
throw new IndexOutOfBoundsException(
// luni.0A=Index: {0}, Size: {1}
Messages.getString("luni.0A", //$NON-NLS-1$
Integer.valueOf(location),
Integer.valueOf(lastIndex - firstIndex)));
}
if (this == collection) {
collection = (ArrayList)clone();
}
Object[] dumparray = collection.toArray();
int growSize = dumparray.length;
if (growSize == 0) {
return false;
}
if (0 < location && location < size) {
if (array.length - size < growSize) {
growForInsert(location, growSize);
} else if ((location < size / 2 && firstIndex > 0)
|| lastIndex > array.length - growSize) {
int newFirst = firstIndex - growSize;
if (newFirst < 0) {
int index = location + firstIndex;
System.arraycopy(array, index, array, index - newFirst,
size - location);
lastIndex -= newFirst;
newFirst = 0;
}
System.arraycopy(array, firstIndex, array, newFirst, location);
firstIndex = newFirst;
} else {
int index = location + firstIndex;
System.arraycopy(array, index, array, index + growSize, size
- location);
lastIndex += growSize;
}
} else if (location == 0) {
growAtFront(growSize);
firstIndex -= growSize;
} else if (location == size) {
if (lastIndex > array.length - growSize) {
growAtEnd(growSize);
}
lastIndex += growSize;
}
System.arraycopy(dumparray, 0, this.array, location + firstIndex,
growSize);
modCount++;
return true;
}
Inserts the objects in the specified collection at the specified location
in this List. The objects are added in the order they are returned from
the collection's iterator. |
public void clear() {
if (firstIndex != lastIndex) {
Arrays.fill(array, firstIndex, lastIndex, null);
firstIndex = lastIndex = 0;
modCount++;
}
}
Removes all elements from this {@code ArrayList}, leaving it empty. |
public Object clone() {
try {
ArrayList< E > newList = (ArrayList< E >) super.clone();
newList.array = array.clone();
return newList;
} catch (CloneNotSupportedException e) {
return null;
}
}
Returns a new {@code ArrayList} with the same elements, the same size and
the same capacity as this {@code ArrayList}. |
public boolean contains(Object object) {
if (object != null) {
for (int i = firstIndex; i < lastIndex; i++) {
if (object.equals(array[i])) {
return true;
}
}
} else {
for (int i = firstIndex; i < lastIndex; i++) {
if (array[i] == null) {
return true;
}
}
}
return false;
}
Searches this {@code ArrayList} for the specified object. |
public void ensureCapacity(int minimumCapacity) {
if (array.length < minimumCapacity) {
if (firstIndex > 0) {
growAtFront(minimumCapacity - array.length);
} else {
growAtEnd(minimumCapacity - array.length);
}
}
}
Ensures that after this operation the {@code ArrayList} can hold the
specified number of elements without further growing. |
public E get(int location) {
if (0 < = location && location < (lastIndex - firstIndex)) {
return array[firstIndex + location];
}
throw new IndexOutOfBoundsException(
// luni.0A=Index: {0}, Size: {1}
Messages.getString("luni.0A", //$NON-NLS-1$
Integer.valueOf(location),
Integer.valueOf(lastIndex - firstIndex)));
}
|
public int indexOf(Object object) {
if (object != null) {
for (int i = firstIndex; i < lastIndex; i++) {
if (object.equals(array[i])) {
return i - firstIndex;
}
}
} else {
for (int i = firstIndex; i < lastIndex; i++) {
if (array[i] == null) {
return i - firstIndex;
}
}
}
return -1;
}
|
public boolean isEmpty() {
return lastIndex == firstIndex;
}
|
public int lastIndexOf(Object object) {
if (object != null) {
for (int i = lastIndex - 1; i >= firstIndex; i--) {
if (object.equals(array[i])) {
return i - firstIndex;
}
}
} else {
for (int i = lastIndex - 1; i >= firstIndex; i--) {
if (array[i] == null) {
return i - firstIndex;
}
}
}
return -1;
}
|
public E remove(int location) {
E result;
int size = lastIndex - firstIndex;
if (0 < = location && location < size) {
if (location == size - 1) {
result = array[--lastIndex];
array[lastIndex] = null;
} else if (location == 0) {
result = array[firstIndex];
array[firstIndex++] = null;
} else {
int elementIndex = firstIndex + location;
result = array[elementIndex];
if (location < size / 2) {
System.arraycopy(array, firstIndex, array, firstIndex + 1,
location);
array[firstIndex++] = null;
} else {
System.arraycopy(array, elementIndex + 1, array,
elementIndex, size - location - 1);
array[--lastIndex] = null;
}
}
if (firstIndex == lastIndex) {
firstIndex = lastIndex = 0;
}
} else {
throw new IndexOutOfBoundsException(
// luni.0A=Index: {0}, Size: {1}
Messages.getString("luni.0A", //$NON-NLS-1$
Integer.valueOf(location),
Integer.valueOf(lastIndex - firstIndex)));
}
modCount++;
return result;
}
Removes the object at the specified location from this list. |
public boolean remove(Object object) {
int location = indexOf(object);
if (location >= 0) {
remove(location);
return true;
}
return false;
}
|
protected void removeRange(int start,
int end) {
if (start >= 0 && start < = end && end < = (lastIndex - firstIndex)) {
if (start == end) {
return;
}
int size = lastIndex - firstIndex;
if (end == size) {
Arrays.fill(array, firstIndex + start, lastIndex, null);
lastIndex = firstIndex + start;
} else if (start == 0) {
Arrays.fill(array, firstIndex, firstIndex + end, null);
firstIndex += end;
} else {
System.arraycopy(array, firstIndex + end, array, firstIndex
+ start, size - end);
int newLast = lastIndex + start - end;
Arrays.fill(array, newLast, lastIndex, null);
lastIndex = newLast;
}
modCount++;
} else {
throw new IndexOutOfBoundsException(
// luni.0B=Array index out of range: {0}
Messages.getString("luni.0B", //$NON-NLS-1$
lastIndex - firstIndex - end));
}
}
Removes the objects in the specified range from the start to the end, but
not including the end index. |
public E set(int location,
E object) {
if (0 < = location && location < (lastIndex - firstIndex)) {
E result = array[firstIndex + location];
array[firstIndex + location] = object;
return result;
}
throw new IndexOutOfBoundsException(
// luni.0A=Index: {0}, Size: {1}
Messages.getString("luni.0A", //$NON-NLS-1$
Integer.valueOf(location),
Integer.valueOf(lastIndex - firstIndex)));
}
Replaces the element at the specified location in this {@code ArrayList}
with the specified object. |
public int size() {
return lastIndex - firstIndex;
}
Returns the number of elements in this {@code ArrayList}. |
public Object[] toArray() {
int size = lastIndex - firstIndex;
Object[] result = new Object[size];
System.arraycopy(array, firstIndex, result, 0, size);
return result;
}
Returns a new array containing all elements contained in this
{@code ArrayList}. |
public T[] toArray(T[] contents) {
int size = lastIndex - firstIndex;
if (size > contents.length) {
Class< ? > ct = contents.getClass().getComponentType();
contents = (T[]) Array.newInstance(ct, size);
}
System.arraycopy(array, firstIndex, contents, 0, size);
if (size < contents.length) {
contents[size] = null;
}
return contents;
}
Returns an array containing all elements contained in this
{@code ArrayList}. If the specified array is large enough to hold the
elements, the specified array is used, otherwise an array of the same
type is created. If the specified array is used and is larger than this
{@code ArrayList}, the array element following the collection elements
is set to null. |
public void trimToSize() {
int size = lastIndex - firstIndex;
E[] newArray = newElementArray(size);
System.arraycopy(array, firstIndex, newArray, 0, size);
array = newArray;
firstIndex = 0;
lastIndex = array.length;
modCount = 0;
}
Sets the capacity of this {@code ArrayList} to be the same as the current
size. |