Generics ArrayList: Bug im Quellcode

Hallo,
ich wollte die Klasse ArrayList selbst implementieren. Meiner Meinung ist nur noch die public boolean contains(X x) Methode falsch. Seit Stunden sitze ich schon dran aber ich finde den Fehler aber nicht. Eine Vermutung, wo der Fehler liegt habe ich schon.
Aus irgendeinem Grund gibt die Abfrage if(!contains(x)) false zurück, obwohl Sie das nicht sollte.
Da, die Zahl 123 im Array enthalten ist.
Also Probleme macht diese Zeile Code im Programm:
System.out.println(l1.remove(new Integer(123)));

Könntet ihr mir helfen meinen Fehler herauszubekommen?

Java:
public class MyArrayList<X> {

    private X[] arrayList;
    int cardinality = 0;
  
    public MyArrayList(int capacity) {
        arrayList = ( X[] ) new Object[capacity];
    }
  
    public int getNextFreeSlot() {
        return cardinality;
    }

    private void setNextFreeSlot(int nextFreeSlot) {
        this.cardinality = nextFreeSlot;
    }
  
    public boolean contains(X x) {
        for(int i=0;i<size();i++) {
            if(arrayList[i].equals(x)) {
                return true;
            }
        }
        return false;
    }

    public void add(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(getNextFreeSlot()+1==arrayList.length) {
            X[] tmpArray= ( X[] ) new Object[arrayList.length];
            for(int i=0;i<tmpArray.length;i++) {
                tmpArray[i]=arrayList[i];
            }
            arrayList= ( X[] )new Object[2*arrayList.length];
            for(int i = 0;i<tmpArray.length;i++) {
                arrayList[i]=tmpArray[i];
            }
        }
        arrayList[cardinality]=x;
        setNextFreeSlot(cardinality+1);;
    }
    public boolean remove(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(!contains(x)) {
            return false;
        }
        int index=0;
        for(int i=0;i<arrayList.length;i++) {
            if(arrayList[i].equals(x)) {
                index=i;
            }
        }
        remove(index);
        return true;
    }
    public X remove(int index) throws IllegalArgumentException{
        if(index <0 || index >= arrayList.length) {
            throw new IllegalArgumentException();
        }
        X deletedObject = arrayList[index];
        for(int i=index;i<arrayList.length-1;i++) {
            arrayList[i]=arrayList[i+1];
        }
        setNextFreeSlot(getNextFreeSlot()-1);
        return deletedObject;
    }
    public X get(int i) throws IllegalArgumentException{
        if(i<0 || i >= arrayList.length) {
            throw new IllegalArgumentException();
        }
        return arrayList[i];
    }
    public void clear() {
        /*for(int i =0;i<nextFreeSlot;i++) {
            arrayList[i]=null;
        }*/
        setNextFreeSlot(0);
    }
    public boolean isEmpty() {
        if(getNextFreeSlot()!=0) {
            return false;
        }
        return true;
    }
  
    public int size() {
        return getNextFreeSlot();
    }
  
    public void print() {
        System.out.print(arrayList[0]);
        for(int i=1;i<arrayList.length;i++) {
            System.out.print(", " + arrayList[i]);
        }
        System.out.print("\n");
    }
}


Java:
public class MyArrayList<X> {

    private X[] arrayList;
    int cardinality = 0;
  
    public MyArrayList(int capacity) {
        arrayList = ( X[] ) new Object[capacity];
    }
  
    public int getNextFreeSlot() {
        return cardinality;
    }

    private void setNextFreeSlot(int nextFreeSlot) {
        this.cardinality = nextFreeSlot;
    }
  
    public boolean contains(X x) {
        for(int i=0;i<size();i++) {
            if(arrayList[i].equals(x)) {
                return true;
            }
        }
        return false;
    }

    public void add(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(getNextFreeSlot()+1==arrayList.length) {
            X[] tmpArray= ( X[] ) new Object[arrayList.length];
            for(int i=0;i<tmpArray.length;i++) {
                tmpArray[i]=arrayList[i];
            }
            arrayList= ( X[] )new Object[2*arrayList.length];
            for(int i = 0;i<tmpArray.length;i++) {
                arrayList[i]=tmpArray[i];
            }
        }
        arrayList[cardinality]=x;
        setNextFreeSlot(cardinality+1);;
    }
    public boolean remove(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(!contains(x)) {
            return false;
        }
        int index=0;
        for(int i=0;i<arrayList.length;i++) {
            if(arrayList[i].equals(x)) {
                index=i;
            }
        }
        remove(index);
        return true;
    }
    public X remove(int index) throws IllegalArgumentException{
        if(index <0 || index >= arrayList.length) {
            throw new IllegalArgumentException();
        }
        X deletedObject = arrayList[index];
        for(int i=index;i<arrayList.length-1;i++) {
            arrayList[i]=arrayList[i+1];
        }
        setNextFreeSlot(getNextFreeSlot()-1);
        return deletedObject;
    }
    public X get(int i) throws IllegalArgumentException{
        if(i<0 || i >= arrayList.length) {
            throw new IllegalArgumentException();
        }
        return arrayList[i];
    }
    public void clear() {
        /*for(int i =0;i<nextFreeSlot;i++) {
            arrayList[i]=null;
        }*/
        setNextFreeSlot(0);
    }
    public boolean isEmpty() {
        if(getNextFreeSlot()!=0) {
            return false;
        }
        return true;
    }
  
    public int size() {
        return getNextFreeSlot();
    }
  
    public void print() {
        System.out.print(arrayList[0]);
        for(int i=1;i<arrayList.length;i++) {
            System.out.print(", " + arrayList[i]);
        }
        System.out.print("\n");
    }
}
 
Hier ist das dazugehörige main-Programm

Java:
public class MyArrayListTest {

    public static void main(String[] args) {
       
        MyArrayList<Integer> l1 = new MyArrayList(3);
       
       
        System.out.println("isEmpty()");
        System.out.println(l1.isEmpty());
        System.out.println("-------------------\n");
       
       
        System.out.println("add(x)");
        l1.add(1);  // 0
        l1.add(2);  // 1
        l1.add(3);  // 2
        l1.add(13); // 3
        l1.add(2);  // 4
        l1.add(12); // 5
        l1.add(123);// 6
        System.out.println("-------------------\n");
       
       
        System.out.println("contains(x) ");
        System.out.println(l1.contains(1));
        System.out.println(l1.contains(2));
        System.out.println(l1.contains(3));
        System.out.println(l1.contains(100));
        System.out.println("-------------------\n");
       
       
        System.out.println("remove(x) ");
        System.out.println(l1.remove(0));
        System.out.println(l1.remove(1));
        System.out.println(l1.remove(6));
        System.out.println(l1.remove(6));
        System.out.println(l1.remove(new Integer(123)));
        System.out.println("-------------------\n");
       
       
        System.out.println("get(x) ");
        System.out.println(l1.get(5));
        System.out.println("-------------------\n");
       
       
        System.out.println("isEmpty()");
        System.out.println(l1.isEmpty());
        System.out.println("-------------------\n");
       
       
        System.out.println("size()");
        System.out.println(l1.size());
        System.out.println("-------------------\n");
       
       
        System.out.println("clear()");
        l1.clear();
        l1.print();
        System.out.println("-------------------\n");
       
       
        System.out.println("size()");
        System.out.println(l1.size());
        System.out.println("-------------------\n");
    }
}
 
Was ich mich noch frage. Ich habe zwei Methoden mit dem gleichen Namen. Woher weiß der Compiler, welche Methode ich aufrufen will? Mt x.remove(3) kann doch der Index 3 oder das Objekt 3 gemeint sein.

public boolean remove(X x)
public X remove(int index)
 
Und wie bekomme ich die andere remove Methode aufgerufen?
Mit x.remove(new Integer(3)); ?

Ein Fehler habe ich schon gefunden. Es muss ein size() hin anstatt dem array.length
for(int i=0;i<size();i++)
 
Ich hab mir den Code mal genauer angeschaut und den einzigen Fehler, den ich vielleicht finde, ist das hier auch size() anstatt arrayList.length hin muss.
if(index <0 || index >= arrayList.length)

Das ist aber kein wirklicher Fehler, weil es doch nicht schlimm ist, wenn man eine null Referenz löscht?
Wo hast du denn noch Fehler gesehen?
 
Es waren alles stellen mit length statt size.
Führte zb dazu, dass löschen von Elementen, die gar nicht mehr in der Liste sind (zb an Index 6, wenn wenige als 6 drin sind), die Liste trotzdem verkürzt hat
 
Ok, ich denke jetzt müsste es stimmen?

Java:
public class MyArrayList<X> {

    private X[] arrayList;
    private int cardinality = 0;
  
    public MyArrayList(int capacity) {
        arrayList = ( X[] ) new Object[capacity];
    }
  
    public int getNextFreeSlot() {
        return cardinality;
    }

    private void setNextFreeSlot(int nextFreeSlot) {
        this.cardinality = nextFreeSlot;
    }
  
    public boolean contains(X x) {
        for(int i=0;i<size();i++) {
            if(arrayList[i].equals(x)) {
                return true;
            }
        }
        return false;
    }

    public void add(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(getNextFreeSlot()+1==arrayList.length) {
            X[] tmpArray= ( X[] ) new Object[arrayList.length];
            for(int i=0;i<tmpArray.length;i++) {
                tmpArray[i]=arrayList[i];
            }
            arrayList= ( X[] )new Object[2*arrayList.length];
            for(int i = 0;i<tmpArray.length;i++) {
                arrayList[i]=tmpArray[i];
            }
        }
        arrayList[cardinality]=x;
        setNextFreeSlot(getNextFreeSlot()+1);;
    }
    public boolean remove(X x) throws IllegalArgumentException{
        if(x==null) {
            throw new IllegalArgumentException();
        }
        if(contains(x)) {
            return false;
        }
        int index=0;
        for(int i=0;i<size();i++) {
            if(arrayList[i].equals(x)) {
                index=i;
            }
        }
        remove(index);
        return true;
    }
    public X remove(int index) throws IllegalArgumentException{
        if(index <0 || index >= size()) {
            throw new IllegalArgumentException();
        }
        X deletedObject = arrayList[index];
        for(int i=index;i<size()-1;i++) {
            arrayList[i]=arrayList[i+1];
        }
        setNextFreeSlot(getNextFreeSlot()-1);
        return deletedObject;
    }
    public X get(int i) throws IllegalArgumentException{
        if(i<0 || i >= size()) {
            throw new IllegalArgumentException();
        }
        return arrayList[i];
    }
    public void clear() {
        /*for(int i =0;i<nextFreeSlot;i++) {
            arrayList[i]=null;
        }*/
        setNextFreeSlot(0);
    }
    public boolean isEmpty() {
        if(getNextFreeSlot()!=0) {
            return false;
        }
        return true;
    }
  
    public int size() {
        return getNextFreeSlot();
    }
  
    public void print() {
        System.out.print(arrayList[0]);
        for(int i=1;i<arrayList.length;i++) {
            System.out.print(", " + arrayList[i]);
        }
        System.out.print("\n");
    }
}

Bei mir wird die statische Methode Integer.of(3) in eclipse nicht angezeigt und in der Java API hab ich die Methode gar nicht gefunden.
 
Zuletzt bearbeitet:

Zurück
Oben