From 233934e9b7ba921b6a08c6fff1bb7463af4b618c Mon Sep 17 00:00:00 2001 From: Matti Date: Thu, 9 May 2024 17:32:46 +0200 Subject: [PATCH] Add 3 2 (Dynamic Size Queue) --- README.md | 1 + src/part3/aufg2/Anwendung.java | 22 ++++++++++++++++++++++ src/part3/aufg2/Schlenglein.java | 28 ++++++++++++++++++++++++++++ src/part3/aufg3/Anwendung.java | 3 +-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c54c66..9e0df2f 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@
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) diff --git a/src/part3/aufg2/Anwendung.java b/src/part3/aufg2/Anwendung.java index 23969e4..472f758 100644 --- a/src/part3/aufg2/Anwendung.java +++ b/src/part3/aufg2/Anwendung.java @@ -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()); } } diff --git a/src/part3/aufg2/Schlenglein.java b/src/part3/aufg2/Schlenglein.java index 5d9b187..c748449 100644 --- a/src/part3/aufg2/Schlenglein.java +++ b/src/part3/aufg2/Schlenglein.java @@ -14,6 +14,7 @@ public class Schlenglein { public void add(T t){ this.myArray[this.numberOfElements] = t; this.numberOfElements++; + attemptDynamicResize(); } public T get(){ @@ -22,9 +23,36 @@ public class Schlenglein { 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); + } + } } diff --git a/src/part3/aufg3/Anwendung.java b/src/part3/aufg3/Anwendung.java index 959a6e0..fb7c374 100644 --- a/src/part3/aufg3/Anwendung.java +++ b/src/part3/aufg3/Anwendung.java @@ -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;