public class MyArrayList<E> {
private int capacity = 10;
private int size = 0;
private E[] values = null;
@SuppressWarnings("unchecked")
public MyArrayList() {
values = (E[]) new Object[capacity];
}
@SuppressWarnings("unchecked")
public MyArrayList(int capacity) {
this.capacity = capacity;
values = (E[]) new Object[this.capacity];
}
public void put(E e) {
if (e == null) {
throw new RuntimeException("The value should not be null.");
}
if (size >= capacity) {
enlargeCapacity();
}
values[size] = e;
size++;
}
public E get(int index) {
if (index >= size) {
throw new RuntimeException("The index:" + index + " is out of band.");
}
return values[index];
}
public void remove(int index) {
if (index >= size) {
throw new RuntimeException("The index:" + index + " is out of band.");
}
for (int i = index; i < size - 1; i++) {
values[i] = values[i + 1];
}
values[size - 1] = null;
size--;
}
@SuppressWarnings("unchecked")
private void enlargeCapacity() {
capacity = capacity * 2;
E[] tmpValues = (E[]) new Object[capacity];
System.arraycopy(values, 0, tmpValues, 0, size);
values = tmpValues;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(values[i]).append(",");
}
if (size > 0) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append("]");
return sb.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
MyArrayList<Integer> myList = new MyArrayList<Integer>();
for(int i=0;i<1000;i++){
myList.put(i);
}
System.out.println(myList.toString());
}
}