import java.util.Arrays;
import java.util.AbstractList;
/**
* This class implements some of the methods of the Java
* ArrayList class.
* @author Koffman & Wolfgang
*/
public class KWArrayList<E> extends AbstractList<E>
{
// Data Fields
/** The default initial capacity */
private static final int INITIAL_CAPACITY = 10;
/** The underlying data array */
private E[] theData;
/** The current size */
private int size = 0;
/** The current capacity */
private int capacity = 0;
/**
* Construct an empty KWArrayList with the default
* initial capacity
*/
public KWArrayList() {
capacity = INITIAL_CAPACITY;
theData = (E[]) new Object[capacity];
}
/**
* Construct an empty KWArrayList with a specified initial capacity
* @param capacity The initial capacity
*/
public KWArrayList(int capacity) {
this.capacity = capacity;
theData = (E[]) new Object[capacity];
}
/**
* Add an entry to the end of the list
* @param theValue - The value to be inserted
*/
public boolean add(E anEntry) {
if (size == capacity) {
reallocate();
}
theData[size] = anEntry;
size++;
return true;
}
/**
* Get a value in the array based on its index.
* @param index - The index of the item desired
* @return The contents of the array at that index
* @throws ArrayIndexOutOfBoundsException - if the index
* is negative or if it is greater than or equal to the
* current size
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index);
}
return theData[index];
}
/**
* Set the value in the array based on its index.
* @param index - The index of the item desired
* @param newValue - The new value to store at this position
* @return The old value at this position
* @throws ArrayIndexOutOfBoundsException - if the index
* is negative or if it is greater than or equal to the
* current size
*/
public E set(int index, E newValue) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index);
}
E oldValue = theData[index];
theData[index] = newValue;
return oldValue;
}
/**
* Add an entry to the data inserting it before the
* item at the specified index.
* @param index - The index of the time that the new
* value it to be inserted in front of.
* @param theValue - The value to be inserted
* @throws ArrayIndexOUtOfBoundsException if index is
* less than zero or greater than size
*/
public void add(int index, E newValue) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index);
}
if(size >= capacity)
reallocate();
for(int i=size; i>index; i--)
theData[i] = theData[i-1];
theData[index] = newValue;
size++;
} /**
* Remove an entry based on its index
* @param index - The index of the entry to be removed
* @return The value removed
* @throws ArrayIndexOutOfBoundsException - if the index
* is negative or if it is greater than or equal to the
* current size
*/
public E remove(int index) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index);
}
E returnValue = theData[index];
for (int i = index; i < size; i++) {
theData[i] = theData[i+1];
}
size--;
return returnValue;
}
/**
* Allocate a new array to hold the directory
*/
private void reallocate() {
capacity = 2 * capacity;
theData = Arrays.copyOf(theData, capacity);
}
/**
* Get the current size of the array
* @return The current size of the array
*/
public int size() {
return size;
}
/**
* Returns the index of the first occurence of the specified element
* in this list, or -1 if this list does not contain the element
* @param item The object to search for
* @returns The index of the first occurence of the specified item
* or -1 if this list does not contain the element
*/
public int indexOf(Object item) {
for (int i = 0; i < size; i++) {
if (theData[i] == null && item == null) {
return i;
}
if (theData[i].equals(item)) {
return i;
}
}
return -1;
}
}
|
add Method

remove Method

public class Test
{
public static void main(String[] args)
{
KWArrayList<String> myarray = new KWArrayList<String>();
myarray.add("Chu");
myarray.add("Chen");
myarray.add("Lu");
myarray.add(1, "Hellon");
for(int i=0; i<myarray.size(); i++)
System.out.println(myarray.get(i));
}
}

static <T> T[] |
copyOf(T[]
original, int newLength)
Copies the specified array, truncating or padding with nulls (if
necessary) so the copy has the specified length.
|
|