Last active
January 4, 2021 20:29
-
-
Save mikebarkmin/dbe1ec2c8f2099bd8ba2a0fe17cb80c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Comparator<T> | |
{ | |
/** | |
* Bsp. compareTo(x, y) | |
* Es wird... | |
* -1 zurückgeben, wenn x < y | |
* 0 zurückgeben, wenn x == y | |
* 1 zurückgeben, wenn x > y | |
*/ | |
int compareTo(T o1, T o2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Consumer<T> | |
{ | |
void accept(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Erweiterung der Generischen Klasse List<ContentType> aus den Materialien zu den zentralen | |
* NRW-Abiturpruefungen im Fach Informatik ab 2018. | |
* | |
* Die Methoden sind an die Methoden der Klasse ArrayList aus der Java | |
* Standardbibliothekt angelehnt. | |
*/ | |
public class CoolList<T> extends List<T> | |
{ | |
/** | |
* Füge der Liste eine übergebene Liste an. | |
* | |
* @param l die Liste, die hinzugefügt werden soll. | |
*/ | |
public void appendAll(List<? extends T> l) { | |
l.toFirst(); | |
while(l.hasAccess()) { | |
this.append(l.getContent()); | |
l.next(); | |
} | |
} | |
/** | |
* Löscht die Liste. | |
*/ | |
public void clear() { | |
this.toFirst(); | |
while(this.hasAccess()) { | |
this.remove(); | |
} | |
} | |
/** | |
* Erstellt eine Kopie der Liste | |
* | |
* @return Kopie der Liste | |
*/ | |
public List<T> clone() { | |
CoolList<T> helper = new CoolList<>(); | |
helper.appendAll(this); | |
return helper; | |
} | |
/** | |
* Überprüft ob ein Objekt in der List ist. | |
* | |
* @param o das Objekt | |
*/ | |
public boolean contains(Object o) { | |
this.toFirst(); | |
while(this.hasAccess()) { | |
if(o == this.getContent()) { | |
return true; | |
} | |
this.next(); | |
} | |
return false; | |
} | |
/** | |
* Gibt das erste Element zurück, welches das Prädikat erfüllt | |
* | |
* @param p das Prädikat | |
*/ | |
public T find(Predicate<T> p) { | |
this.toFirst(); | |
while(this.hasAccess()) { | |
T current = this.getContent(); | |
if(current != null && p.test(current)) { | |
return current; | |
} | |
this.next(); | |
} | |
return null; | |
} | |
/** | |
* Gibt alle Elemente zurück, die das Prädikat erfüllen. | |
* | |
* @param p das Prädikat | |
*/ | |
public List<T> findAll(Predicate<T> p) { | |
this.toFirst(); | |
List<T> findings = new List<>(); | |
while(this.hasAccess()) { | |
T current = this.getContent(); | |
if(p.test(current)) { | |
findings.append(current); | |
} | |
} | |
return findings; | |
} | |
/** | |
* Verwendet einen Verbraucher auf jedes Element der Liste an. | |
* | |
* @param action der Verbraucher | |
*/ | |
public void forEach(Consumer<? super T> action) { | |
this.toFirst(); | |
while(this.hasAccess()) { | |
action.accept(this.getContent()); | |
this.next(); | |
} | |
} | |
/** | |
* Gibt das i-te Element der Liste zurück. | |
* | |
* @param i Index | |
* | |
* @return das i-te Element oder null, wenn der Index nicht exisitert. | |
*/ | |
public T get(int i) { | |
this.toFirst(); | |
int currentIndex = 0; | |
while(this.hasAccess()) { | |
if (i == currentIndex) { | |
break; | |
} | |
this.next(); | |
currentIndex++; | |
} | |
if (i < currentIndex) { | |
return null; | |
} | |
return this.getContent(); | |
} | |
/** | |
* Gib den Index des Objekts zurück oder -1, wenn das Objekt nicht in der List ist. | |
*/ | |
public int indexOf(Object o) { | |
this.toFirst(); | |
int index = -1; | |
while(this.hasAccess()) { | |
index += 1; | |
if (this.getContent() == o) { | |
return index; | |
} | |
this.next(); | |
} | |
return index; | |
} | |
/** | |
* Gib den letzten Index des Objekts zurück oder -1, wenn das Objekt nicht in der List ist. | |
*/ | |
public int lastIndexOf(Object o) { | |
this.toFirst(); | |
int index = -1; | |
int lastIndex = -1; | |
while(this.hasAccess()) { | |
index += 1; | |
if (this.getContent() == o) { | |
lastIndex = index; | |
} | |
} | |
return lastIndex; | |
} | |
/** | |
* Entfernt das Element am übergebenen Index. | |
* | |
* @param index der Index | |
* | |
* @return true wenn ein Element entfernt wurde, false andernfalls. | |
*/ | |
public boolean remove(int index) { | |
this.toFirst(); | |
int currentIndex = 0; | |
while(this.hasAccess()) { | |
if (currentIndex == index) { | |
this.remove(); | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Entfernt das erste Vorkommen des Objekts aus der Liste. | |
* | |
* @param o das Objekt | |
* | |
* @return true wenn das Objekt entfernt wurde, false andernfalls. | |
*/ | |
public boolean remove(Object o) { | |
this.toFirst(); | |
while(this.hasAccess()) { | |
if(this.getContent() == o) { | |
this.remove(); | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Entfernt alle Elemente von der List, die in der übergebenen vorkommen. | |
* | |
* @param l Liste zu löschender Objekte | |
* | |
* @return true wenn mindestens ein Objekt entfernt wurde, false andernfalls. | |
*/ | |
public boolean removeAll(List<?> l) { | |
l.toFirst(); | |
boolean removedOne = false; | |
while(l.hasAccess()) { | |
if (this.remove(l.getContent())) { | |
removedOne = true; | |
} | |
} | |
return removedOne; | |
} | |
/** | |
* Entfernt alle Elemente von der List die das Prädikat erfüllen. | |
* | |
* @param p das Prädikat | |
* | |
* @return true wenn mindestens ein Objekt entfernt wurde, false andernfalls. | |
*/ | |
public boolean removeIf(Predicate<? super T> p) { | |
this.toFirst(); | |
boolean removedOne = false; | |
while(this.hasAccess()) { | |
if(p.test(this.getContent())) { | |
this.remove(); | |
removedOne = true; | |
} else { | |
this.next(); | |
} | |
} | |
return removedOne; | |
} | |
/** | |
* Gibt die Länge der Liste zurück. | |
* | |
* @return die Länger der Liste | |
*/ | |
public int size() { | |
this.toFirst(); | |
int size = 0; | |
while(this.hasAccess()) { | |
this.next(); | |
size++; | |
} | |
return size; | |
} | |
/** | |
* Sortiert die Liste anhand eines Komparator. | |
* | |
* @param c der Komparator | |
*/ | |
public void sort(Comparator<? super T> c) { | |
List<T> helper = new List<T>(); | |
while(!this.isEmpty()) { | |
this.toFirst(); | |
T max = this.getContent(); | |
while(this.hasAccess()) { | |
T current = this.getContent(); | |
if(c.compareTo(current, max) > 1) { | |
max = current; | |
} | |
this.next(); | |
} | |
this.toFirst(); | |
while (this.hasAccess()) { | |
T current = this.getContent(); | |
if (c.compareTo(max, current) == 0) { | |
this.remove(); | |
} | |
this.next(); | |
} | |
helper.append(max); | |
} | |
this.concat(helper); | |
} | |
/** | |
* Gibt eine Teilliste zurück, die einen bestimmten Bereich dieser Liste abbildet. | |
* Der Bereich ist definiert duch fromIndex bis toIndex. | |
* | |
* @param fromIndex der Start-Index (einschließlich) | |
* @param toIndex der End-Index (ausschließlich) | |
* | |
* @throws IndexOutOfBoundsException wenn der Endpunkt nicht im Bereich ist (fromIndex < 0 || toIndex > size) | |
* @throws IllegalArgumentException wenn die Endpunkte eine falsche Reihenfolge haben (fromIndex > toIndex) | |
* | |
* @return die Teilliste im Bereich [fromIndex, toIndex) | |
*/ | |
public List<T> subList(int fromIndex, int toIndex) throws IndexOutOfBoundsException, IllegalArgumentException { | |
List<T> helper = new List<T>(); | |
if (fromIndex < 0 || toIndex > size()) { | |
throw new IndexOutOfBoundsException(); | |
} | |
if (fromIndex > toIndex) { | |
throw new IllegalArgumentException(); | |
} | |
this.toFirst(); | |
int index = 0; | |
while(this.hasAccess()) { | |
if (index >= fromIndex && index < toIndex) { | |
helper.append(this.getContent()); | |
} | |
index += 1; | |
this.next(); | |
} | |
return helper; | |
} | |
/** | |
* Gib ein Array, welches aus der Liste generiert wurde, zurück. | |
* | |
* @return ein Array | |
*/ | |
public T[] toArray() { | |
T[] a = new T[this.size()]; | |
this.toFirst(); | |
int index = 0; | |
while(this.hasAccess()) { | |
a[index] = this.getContent(); | |
index += 1; | |
this.next(); | |
} | |
return a; | |
} | |
public int reduceToInt(ToIntFunction<? super T> f) { | |
int reducation = 0; | |
this.toFirst(); | |
while(this.hasAccess()) { | |
T content = this.getContent(); | |
reducation += f.applyAsInt(content); | |
} | |
return reducation; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Predicate<T> | |
{ | |
public boolean test(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ToIntFunction<T> | |
{ | |
public int applyAsInt(T t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment