Source für Aufgabe 2 eingefügt

This commit is contained in:
Johannes Theiner 2018-11-12 08:33:01 +01:00
parent b58c50cc54
commit 88e27fad0f
5 changed files with 229 additions and 2 deletions

11
pom.xml
View File

@ -13,8 +13,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>8</source> <source>10</source>
<target>8</target> <target>10</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
@ -37,6 +37,13 @@
<version>RELEASE</version> <version>RELEASE</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,66 @@
package xyz.joethei.studium.algodat.praktikum.blatt2;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
public class ArrayList<E> implements List<E> {
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<E> 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();
}
};
}
}

View File

@ -0,0 +1,81 @@
package xyz.joethei.studium.algodat.praktikum.blatt2;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
public class LinkedList<E> implements List<E> {
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<E> 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();
}
};
}
}

View File

@ -0,0 +1,8 @@
package xyz.joethei.studium.algodat.praktikum.blatt2;
public interface List<E> extends Iterable<E>{
boolean add(E e);
E get(int index);
int size();
}

View File

@ -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<Integer> 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<String> bList = new LinkedList<>();
for (int i = 1; i <= n; i++) {
bList.add(i + "<EFBFBD> = " + 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<Integer> 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<String> dList = new ArrayList<>();
for (int i = 1; i <= n; i++) {
dList.add(i + "<EFBFBD> = " + 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");
}
}