From 88e27fad0f8c168c45ed0f7b1a52ed4e881df1ce Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 12 Nov 2018 08:33:01 +0100 Subject: [PATCH] =?UTF-8?q?Source=20f=C3=BCr=20Aufgabe=202=20eingef=C3=BCg?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 ++- .../algodat/praktikum/blatt2/ArrayList.java | 66 +++++++++++++++ .../algodat/praktikum/blatt2/LinkedList.java | 81 +++++++++++++++++++ .../algodat/praktikum/blatt2/List.java | 8 ++ .../algodat/praktikum/blatt2/Main.java | 65 +++++++++++++++ 5 files changed, 229 insertions(+), 2 deletions(-) create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/Main.java diff --git a/pom.xml b/pom.xml index ea16109..940bd3b 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 10 + 10 @@ -37,6 +37,13 @@ RELEASE test + + org.jetbrains + annotations + 13.0 + compile + + diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java new file mode 100644 index 0000000..6616db3 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/ArrayList.java @@ -0,0 +1,66 @@ +package xyz.joethei.studium.algodat.praktikum.blatt2; + +import org.jetbrains.annotations.NotNull; + +import java.util.Iterator; + +public class ArrayList implements List { + + private static final int MAXSIZE = 32; + private int size; + + private E[] a; + + ArrayList() { + size = 0; + a = (E[]) new Object[MAXSIZE]; + } + + public boolean add(E e) { + if (size < MAXSIZE) { + a[size] = e; + size++; + return true; + } else { + return false; + } + + } + + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + return a[index]; + } + + public int size() { + return size; + } + + @NotNull + public Iterator iterator() { + return new Iterator<>() { + int next = 0; + int current; + + public boolean hasNext() { + return next < size; + } + + public E next() { + current = next; + if (next < size) { + next++; + } + return a[current]; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + +} diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java new file mode 100644 index 0000000..78bacb3 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/LinkedList.java @@ -0,0 +1,81 @@ +package xyz.joethei.studium.algodat.praktikum.blatt2; + +import org.jetbrains.annotations.NotNull; + +import java.util.Iterator; + +public class LinkedList implements List { + private class Wrapper { + E e; + Wrapper succ; + + Wrapper(E e) { + this.e = e; + succ = null; + } + } + + private Wrapper first; + private Wrapper last; + private int size; + + LinkedList() { + first = null; + last = null; + size = 0; + } + + public boolean add(E e) { + Wrapper w = new Wrapper(e); + if (size == 0) { + first = w; + last = w; + } else { + last.succ = w; + last = w; + } + size++; + return true; + } + + public E get(int index) { + Wrapper wRun; + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + wRun = this.first; + for (int iRun = 0; iRun < index; iRun++) { + wRun = wRun.succ; + } + return wRun.e; + } + + public int size() { + return size; + } + + @NotNull + public Iterator iterator() { + return new Iterator<>() { + Wrapper next = first; + Wrapper current; + + public boolean hasNext() { + return next != null; + } + + public E next() { + current = next; + if (next != null) { + next = next.succ; + } + return current.e; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + +} diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java new file mode 100644 index 0000000..c29a948 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/List.java @@ -0,0 +1,8 @@ +package xyz.joethei.studium.algodat.praktikum.blatt2; + + +public interface List extends Iterable{ + boolean add(E e); + E get(int index); + int size(); +} diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/Main.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/Main.java new file mode 100644 index 0000000..2da7a51 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt2/Main.java @@ -0,0 +1,65 @@ +package xyz.joethei.studium.algodat.praktikum.blatt2; + +public class Main { + + public static void main(String[] args) { + + int n = 10; + if (args.length > 0) { + n = Integer.parseInt(args[0]); + } + + List aList = new LinkedList<>(); + + for (int i = 1; i <= n; i++) { + aList.add(i * i); + } + + /* Ausgabe der Integer-LinkedList aList */ + System.out.println("--- Integer-LinkedList aList ---"); + for (int i = 0; i < aList.size(); i++) { + System.out.print(aList.get(i) + " "); + } + System.out.println("\n"); + + List bList = new LinkedList<>(); + + for (int i = 1; i <= n; i++) { + bList.add(i + "� = " + i * i); + } + + /* Ausgabe der String-LinkedList bList */ + System.out.println("--- String-LinkedList bList ---"); + for (String aBList : bList) { + System.out.print(aBList + " "); + } + System.out.println("\n"); + + List cList = new ArrayList<>(); + + for (int i = 1; i <= n; i++) { + cList.add(i * i); + } + + /* Ausgabe der Integer-ArrayList cList */ + System.out.println("--- Integer-ArrayList cList ---"); + for (int i = 0; i < cList.size(); i++) { + System.out.print(cList.get(i) + " "); + } + System.out.println("\n"); + + List dList = new ArrayList<>(); + + for (int i = 1; i <= n; i++) { + dList.add(i + "� = " + i * i); + } + + /* Ausgabe der String-ArrayList dList */ + System.out.println("--- String-ArrayList dList ---"); + for (String aDList : dList) { + System.out.print(aDList + " "); + } + System.out.println("\n"); + + } +}