Add 3 2 (Dynamic Size Queue)
This commit is contained in:
parent
f5319538ad
commit
233934e9b7
@ -10,6 +10,7 @@
|
||||
<br>Sowohl SourceCode, PDF als auch Word Datei
|
||||
- VL 2 Aufgabe 1 (Erstellen von Testdaten)
|
||||
- VL 2 Aufgabe 2 (StringTokenizer)
|
||||
- VL 3 Aufgabe 2 (Dynamischer Speicher einer Queue)
|
||||
- VL 3 Aufgabe 3 (ArrayList vs LinkedList)
|
||||
|
||||
|
||||
|
@ -11,6 +11,28 @@ package part3.aufg2;
|
||||
|
||||
public class Anwendung {
|
||||
public static void main(String[] args){
|
||||
Schlenglein kleinesSchlenglein = new Schlenglein();
|
||||
|
||||
System.out.println(kleinesSchlenglein.getArrayLenght());
|
||||
|
||||
for (int i=0; i<43; i++){
|
||||
kleinesSchlenglein.add("a");
|
||||
}
|
||||
System.out.println(kleinesSchlenglein.getArrayLenght());
|
||||
|
||||
for (int i=0; i<35; i++){
|
||||
kleinesSchlenglein.get();
|
||||
}
|
||||
System.out.println(kleinesSchlenglein.getArrayLenght());
|
||||
|
||||
for (int i=0; i<4321; i++){
|
||||
kleinesSchlenglein.add("a");
|
||||
}
|
||||
System.out.println(kleinesSchlenglein.getArrayLenght());
|
||||
|
||||
for (int i=0; i<9999; i++){
|
||||
kleinesSchlenglein.get();
|
||||
}
|
||||
System.out.println(kleinesSchlenglein.getArrayLenght());
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ public class Schlenglein <T>{
|
||||
public void add(T t){
|
||||
this.myArray[this.numberOfElements] = t;
|
||||
this.numberOfElements++;
|
||||
attemptDynamicResize();
|
||||
}
|
||||
|
||||
public T get(){
|
||||
@ -22,9 +23,36 @@ public class Schlenglein <T>{
|
||||
this.myArray[i] = this.myArray[i+1];
|
||||
}
|
||||
this.numberOfElements--;
|
||||
attemptDynamicResize();
|
||||
return retValue;
|
||||
}
|
||||
public int size(){
|
||||
return this.numberOfElements;
|
||||
}
|
||||
|
||||
public int getArrayLenght(){return this.maxSize;}
|
||||
public void attemptDynamicResize(){
|
||||
if (numberOfElements < 0.2*maxSize){
|
||||
// Kleiner Machen
|
||||
Object[] temp = new Object[maxSize];
|
||||
|
||||
System.arraycopy(myArray, 0, temp, 0, maxSize);
|
||||
|
||||
maxSize /= 2;
|
||||
if (maxSize < 20){maxSize = 20;}
|
||||
|
||||
System.arraycopy(temp, 0, myArray, 0, maxSize);
|
||||
|
||||
} else if (numberOfElements > 0.8*maxSize) {
|
||||
// Größer Machen
|
||||
Object[] temp = new Object[maxSize];
|
||||
|
||||
System.arraycopy(myArray, 0, temp, 0, maxSize);
|
||||
|
||||
maxSize -=- maxSize;
|
||||
myArray = new Object[maxSize];
|
||||
|
||||
System.arraycopy(temp, 0, myArray, 0, maxSize / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ import java.util.LinkedList;
|
||||
* jeweils den Mittelwert!
|
||||
*
|
||||
*
|
||||
*
|
||||
* Verwenden Sie für die Zeitmessung folgenden Code:
|
||||
*
|
||||
* long timeStart = System.currentTimeMillis();
|
||||
@ -58,7 +57,7 @@ public class Anwendung {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int stringsToAdd = 50_000_000;
|
||||
int stringsToAdd = 500_000;
|
||||
long totalTimeSpent = 0;
|
||||
int measurements = 10;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user