Java: Add-Funktion selber schreiben?
Hallo zusammen.
Ich versuche zurzeit die Arraylist selber zu schreiben und komme bei der Add-Funktion nicht weiter, da mir diese eine Array-OutOfBound-Exception schmeißt.
Was kann ich tun?
Hier der Code:
public class MyArrayList<E> implements MyArrayListInterface<E> {
/**
* Fields
*/
private E[] list;
private int size, capacity, increment, l;
private Array String;
/**
* Methods
*
* @param c
* @param inc
*/
@SuppressWarnings("unchecked")
public MyArrayList(int c, int inc) {
if (c > 0) {
this.capacity = c;
}
else {
this.capacity = 10;
}
list = (E[]) new Object[c];
size = 0;
if (inc > 0) {
this.increment = inc;
}
else {
this.increment = 1;
}
}
@Override
@SuppressWarnings("unchecked")
public void add(E e) {
for (int i = 0; i < list.length; i++) {
if (list[i] == null) {
list[i] = e;
}
else {
E[] list1 = (E[]) new Object[capacity + increment];
System.arraycopy(list, 0, list1, 0, size);
list1[i] = e;
}
size++;
System.out.println(Arrays.toString(list));
}
}