Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package name.panitz.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6.  
  7. public class AL<A> {
  8.     @SuppressWarnings("unchecked")
  9.     private A[] as = (A[]) new Object[10];
  10.     int length = 0;
  11.  
  12.     public void add(A a) {
  13.         if (as.length <= length)
  14.             größeresRegalKaufen();
  15.         as[length] = a;
  16.         length = length + 1;
  17.     }
  18.  
  19.     @SuppressWarnings("unchecked")
  20.     private void größeresRegalKaufen() {
  21.         A[] neueRegal = (A[]) new Object[length + 10];
  22.         for (int i = 0; i < as.length; i++) {
  23.             neueRegal[i] = as[i];
  24.         }
  25.         as = neueRegal;
  26.     }
  27.  
  28.     public A get(int i) {
  29.         return as[i];  
  30.     }
  31.  
  32.     public void addAll(java.util.Collection<? extends A> os) {
  33.         for (A a : os)
  34.             add(a);
  35.     }
  36.  
  37.     public boolean contains(A o) {
  38.         for (A a : as)
  39.             if (a.equals(o))
  40.                 return true;
  41.        
  42.         return false;
  43.     }
  44.  
  45.     public void remove(int i) {
  46.         if (0 <= i && i < length) {
  47.             for (int j = i; j < length; j++)
  48.                 as[j] = as[j + 1];
  49.        
  50.             length--;
  51.         }
  52.     }
  53.  
  54.     public void add(A o, int i) {
  55.         add(o);
  56.         for (int j = length - 2; j >= i; j--)
  57.             as[j + 1] = as[j];
  58.         as[i] = o;
  59.     }
  60.  
  61.     public AL<A> sublist(int from, int length) {
  62.         AL<A> localAL = new AL<>();    
  63.         for (int i = from; i < from + length && i < this.length; i++)
  64.             localAL.add(as[i]);
  65.        
  66.         return localAL;
  67.     }
  68.  
  69.     public void forEach(java.util.function.Consumer<A> f) {
  70.         for (A a : as)
  71.             f.accept(a);
  72.     }
  73.  
  74.     public <B> AL<B> map(java.util.function.Function<A, B> f) {
  75.         AL<B> targetList = new AL<>();
  76.        
  77.         for (int i = 0; i < length; i++)
  78.             targetList.add(f.apply(as[i]));
  79.        
  80.         return targetList;
  81.     }
  82.  
  83.     public void sortBy(java.util.Comparator<? super A> comp) {
  84.         for (int i = 0; i < length - 1; i++) {
  85.             for (int j = 0; j < length - i - 1; j++) {
  86.                 if (comp.compare(as[j], as[j + 1]) > 0) {
  87.                     A temp = as[j];
  88.                     as[j] = as[j + 1];
  89.                     as[j + 1] = temp;
  90.                 }
  91.             }
  92.         }
  93.     }
  94.    
  95.     @Override
  96.     public String toString() {
  97.         return "AL { " + length + " " + Arrays.toString(as) + " }";
  98.     }
  99.    
  100.     public static void main(String[] args) {
  101.         AL<String> al = new AL<>();
  102.         for (char c = 'A'; c <= 'Z'; c++)
  103.             al.add(Character.toString(c));
  104.        
  105.         Collection<String> collection = new ArrayList<String>();
  106.         collection.add("ä");
  107.         collection.add("ö");
  108.         collection.add("ü");
  109.         al.addAll(collection);
  110.        
  111.         System.out.println(al);
  112.        
  113.         System.out.println(al.contains("ä"));
  114.        
  115.         al.remove(28);
  116.         al.remove(28);
  117.         System.out.println(al);
  118.        
  119.         al.add("X", 5);
  120.         al.add("X", 5);
  121.         System.out.println(al);
  122.        
  123.         al.add("X", 5);
  124.         System.out.println(al);
  125.        
  126.         AL<String> sl = al.sublist(35, 10);
  127.         System.out.println(sl);
  128.        
  129.         al.forEach(s -> System.out.print(s + " "));
  130.         System.out.println();
  131.        
  132.         AL<Integer> integerList = new AL<>();
  133.         for (int i = 1; i < 10; i++)
  134.             integerList.add(i);
  135.        
  136.         AL<Double> doubleList = integerList.map(i -> (double)i / 2);
  137.         System.out.println(doubleList);
  138.        
  139.         AL<Integer> sortList = new AL<>();
  140.         for (int i = 0; i < 15; i++)
  141.             sortList.add(new java.util.Random().nextInt(100));
  142.         System.out.println(sortList);
  143.        
  144.         sortList.sortBy((i1, i2) -> Integer.compare(i1, i2));
  145.         System.out.println(sortList);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement