Files
Jander_Semester2/uebung_02/src/util/IntArrayList.java
2026-05-23 22:08:01 +02:00

56 lines
1.3 KiB
Java

package util;
public class IntArrayList {
int lenght = 4;
int lastUnfilledPos = 0;
int[] arr = new int[4];
public IntArrayList(){}
public void add(int a){
if (lastUnfilledPos < lenght){
arr[lastUnfilledPos] = a;
lastUnfilledPos ++;
}
else {
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;
}
}