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?
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");
}
}