74 lines
1.7 KiB
Java
74 lines
1.7 KiB
Java
package Semester2;
|
|
|
|
public class Vorlesung2part2<T> {
|
|
private Object[] myArray;
|
|
private int numberOfElements;
|
|
private int maxSize;
|
|
|
|
|
|
public static void main(String[] args) {
|
|
Vorlesung2part2 testObjekt = new Vorlesung2part2<>();
|
|
for (int i = 0; i < 300; i++) {
|
|
if (i < 150) {
|
|
testObjekt.add(new Vorlesung2part2<>());
|
|
} else {
|
|
testObjekt.get();
|
|
}
|
|
|
|
System.out.println(i);
|
|
}
|
|
}
|
|
|
|
|
|
public Vorlesung2part2() {
|
|
this.numberOfElements = 0;
|
|
this.maxSize = 100;
|
|
myArray = new Object[this.maxSize];
|
|
}
|
|
|
|
|
|
public void add(T t) {
|
|
this.myArray[this.numberOfElements] = t;
|
|
this.numberOfElements++;
|
|
this.check();
|
|
}
|
|
|
|
|
|
public T get() {
|
|
T retValue = (T) this.myArray[0];
|
|
for (int i=0; i<this.numberOfElements; i++) {
|
|
this.myArray[i] = this.myArray[i+1];
|
|
}
|
|
this.numberOfElements--;
|
|
this.check();
|
|
return retValue;
|
|
}
|
|
|
|
|
|
public int size() {
|
|
return this.numberOfElements;
|
|
}
|
|
|
|
|
|
public void check() {
|
|
Object[] temp = myArray;
|
|
if ((float)this.numberOfElements/(float)this.maxSize > 0.8) {
|
|
this.maxSize *= 2;
|
|
temp = myArray;
|
|
} else if (((float)this.numberOfElements/(float)this.maxSize < 0.2) && (this.maxSize >= 40)) {
|
|
this.maxSize /= 2;
|
|
temp = myArray;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
myArray = new Object[this.maxSize];
|
|
for (int i = 0; i < this.numberOfElements; i++) {
|
|
myArray[i] = temp[i];
|
|
System.out.println(myArray[i]);
|
|
}
|
|
|
|
System.out.println("New size: " + this.maxSize);
|
|
}
|
|
}
|