Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package name.panitz.util;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- public class AL<A> {
- @SuppressWarnings("unchecked")
- private A[] as = (A[]) new Object[10];
- int length = 0;
- public void add(A a) {
- if (as.length <= length)
- größeresRegalKaufen();
- as[length] = a;
- length = length + 1;
- }
- @SuppressWarnings("unchecked")
- private void größeresRegalKaufen() {
- A[] neueRegal = (A[]) new Object[length + 10];
- for (int i = 0; i < as.length; i++) {
- neueRegal[i] = as[i];
- }
- as = neueRegal;
- }
- public A get(int i) {
- return as[i];
- }
- public void addAll(java.util.Collection<? extends A> os) {
- for (A a : os)
- add(a);
- }
- public boolean contains(A o) {
- for (A a : as)
- if (a.equals(o))
- return true;
- return false;
- }
- public void remove(int i) {
- if (0 <= i && i < length) {
- for (int j = i; j < length; j++)
- as[j] = as[j + 1];
- length--;
- }
- }
- public void add(A o, int i) {
- add(o);
- for (int j = length - 2; j >= i; j--)
- as[j + 1] = as[j];
- as[i] = o;
- }
- public AL<A> sublist(int from, int length) {
- AL<A> localAL = new AL<>();
- for (int i = from; i < from + length && i < this.length; i++)
- localAL.add(as[i]);
- return localAL;
- }
- public void forEach(java.util.function.Consumer<A> f) {
- for (A a : as)
- f.accept(a);
- }
- public <B> AL<B> map(java.util.function.Function<A, B> f) {
- AL<B> targetList = new AL<>();
- for (int i = 0; i < length; i++)
- targetList.add(f.apply(as[i]));
- return targetList;
- }
- public void sortBy(java.util.Comparator<? super A> comp) {
- for (int i = 0; i < length - 1; i++) {
- for (int j = 0; j < length - i - 1; j++) {
- if (comp.compare(as[j], as[j + 1]) > 0) {
- A temp = as[j];
- as[j] = as[j + 1];
- as[j + 1] = temp;
- }
- }
- }
- }
- @Override
- public String toString() {
- return "AL { " + length + " " + Arrays.toString(as) + " }";
- }
- public static void main(String[] args) {
- AL<String> al = new AL<>();
- for (char c = 'A'; c <= 'Z'; c++)
- al.add(Character.toString(c));
- Collection<String> collection = new ArrayList<String>();
- collection.add("ä");
- collection.add("ö");
- collection.add("ü");
- al.addAll(collection);
- System.out.println(al);
- System.out.println(al.contains("ä"));
- al.remove(28);
- al.remove(28);
- System.out.println(al);
- al.add("X", 5);
- al.add("X", 5);
- System.out.println(al);
- al.add("X", 5);
- System.out.println(al);
- AL<String> sl = al.sublist(35, 10);
- System.out.println(sl);
- al.forEach(s -> System.out.print(s + " "));
- System.out.println();
- AL<Integer> integerList = new AL<>();
- for (int i = 1; i < 10; i++)
- integerList.add(i);
- AL<Double> doubleList = integerList.map(i -> (double)i / 2);
- System.out.println(doubleList);
- AL<Integer> sortList = new AL<>();
- for (int i = 0; i < 15; i++)
- sortList.add(new java.util.Random().nextInt(100));
- System.out.println(sortList);
- sortList.sortBy((i1, i2) -> Integer.compare(i1, i2));
- System.out.println(sortList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement