uebung_02: complete IntArrayList with get, size, sort, remove and finish add

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-23 22:08:01 +02:00
parent 69b1531a53
commit 964e6a0afb

View File

@@ -14,7 +14,42 @@ public class IntArrayList {
} }
else { else {
int[] arrTemp = new int[lenght*2]; int[] arrTemp = new int[lenght*2];
System.arraycopy(arr, 0, arrTemp, 0, lenght);
arr = arrTemp;
lenght = lenght * 2;
arr[lastUnfilledPos] = a;
lastUnfilledPos ++;
}
}
} public int get(int pos){
if (pos < 0 || pos >= lastUnfilledPos){
return -1;
}
return arr[pos];
}
public int size(){
return lastUnfilledPos;
}
public void sort(){
int[] view = new int[lastUnfilledPos];
System.arraycopy(arr, 0, view, 0, lastUnfilledPos);
Sorter sorter = new Sorter();
sorter.mergeSort(view);
System.arraycopy(view, 0, arr, 0, lastUnfilledPos);
}
public int remove(int pos){
if (pos < 0 || pos >= lastUnfilledPos){
return -1;
}
int removed = arr[pos];
for (int i = pos; i < lastUnfilledPos -1; i++){
arr[i] = arr[i+1];
}
lastUnfilledPos --;
return removed;
} }
} }