From d496f4b5666b844e4dd97eeced5efad76d56d599 Mon Sep 17 00:00:00 2001 From: Aminos061 <121253097+Aminos061@users.noreply.github.com> Date: Tue, 7 May 2024 14:10:06 +0200 Subject: [PATCH] test --- .../Generics_Grundlagen/Uebung1.java | 19 +++ .../Generics_Grundlagen/Uebung2.java | 134 ++++++++++++++++++ bin/.gitignore | 11 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 Moodle_Uebungen/Generics_Grundlagen/Uebung1.java create mode 100644 Moodle_Uebungen/Generics_Grundlagen/Uebung2.java diff --git a/Moodle_Uebungen/Generics_Grundlagen/Uebung1.java b/Moodle_Uebungen/Generics_Grundlagen/Uebung1.java new file mode 100644 index 0000000..a5da6dc --- /dev/null +++ b/Moodle_Uebungen/Generics_Grundlagen/Uebung1.java @@ -0,0 +1,19 @@ +package Generics_Grundlagen; + +import java.util.ArrayList; +import java.util.Random; + +public class Uebung1 { + + public static void main(String[] args) { + ArrayList zahlen = new ArrayList<>(); + Random random = new Random(); + while (zahlen.size() < 100) { + zahlen.add(random.nextInt(99) + 1); + } + zahlen.sort(null); + System.out.println(zahlen); + + } + +} diff --git a/Moodle_Uebungen/Generics_Grundlagen/Uebung2.java b/Moodle_Uebungen/Generics_Grundlagen/Uebung2.java new file mode 100644 index 0000000..2178ced --- /dev/null +++ b/Moodle_Uebungen/Generics_Grundlagen/Uebung2.java @@ -0,0 +1,134 @@ +package Generics_Grundlagen; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class Uebung2 implements Iterable { + private Object[] array; + private int size; + private int capacity; + + public Uebung2(int capacity) { + if (capacity <= 0) { + throw new IllegalArgumentException("Capacity must be greater than zero"); + } + this.capacity = capacity; + this.array = new Object[capacity]; + this.size = 0; + } + + public int size() { + return size; + } + + public int capacity() { + return capacity; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean isFull() { + return size == capacity; + } + + public void add(E element) { + if (isFull()) { + throw new IllegalStateException("Array is full"); + } + array[size++] = element; + } + + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + @SuppressWarnings("unchecked") + E element = (E) array[index]; + return element; + } + + public void remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } +// System.arraycopy(array, index + 1, array, index, size - index - 1); +// array[--size] = null; + + array[index] = null; + } + + public void clear() { + Arrays.fill(array, null); + size = 0; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + + @Override + public boolean hasNext() { + //return currentIndex < size -1; + return currentIndex < size; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + @SuppressWarnings("unchecked") + E element = (E) array[currentIndex]; + currentIndex++; + return element; + } + }; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < size; i++) { + sb.append(array[i]); + if (i < size - 1) { + sb.append(", "); + } + } + sb.append("]"); + return sb.toString(); + } + + // Test + public static void main(String[] args) { + Uebung2 array = new Uebung2<>(5); + array.add(1); + array.add(2); + array.add(3); + array.add(4); + array.add(5); + System.out.println("Array: " + array); + System.out.println("Size: " + array.size()); + System.out.println("Is Full: " + array.isFull()); + + try { + array.add(6); // Should throw IllegalStateException + } catch (IllegalStateException e) { + System.out.println("Caught Exception: " + e.getMessage()); + } + + array.remove(2); + System.out.println("Array after removing element at index 2: " + array); + System.out.println("Size after removal: " + array.size()); + System.out.println("Is Empty: " + array.isEmpty()); + + array.clear(); + System.out.println("Array after clearing: " + array); + System.out.println("Size after clearing: " + array.size()); + System.out.println("Is Empty after clearing: " + array.isEmpty()); + } +} + diff --git a/bin/.gitignore b/bin/.gitignore index 871ad7d..cc72566 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,2 +1,11 @@ -/Uebung2_IO/ +/.classpath +/.project /Testat1/ +/buchungen.text +/test.txt +/Lernen/ +/Uebung1_Vererbung/ +/Uebung2_IO/ +/Uebung3_Buchungen/ +/Generics_Grundlagen/ +/Collection/