Advertisement
Filip_Markoski

[NP] 4.4 Генерички контејнер со променлива должина (Solved)

Oct 25th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.88 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.LinkedList;
  4. import java.util.stream.IntStream;
  5.  
  6. class ResizableArray<T> {
  7.     private T[] elements;
  8.     private int size;
  9.     private int maxSize;
  10.  
  11.     @Override
  12.     public String toString() {
  13.         StringBuffer sb = new StringBuffer();
  14.         for (int i = 0; i < size; i++) {
  15.             sb.append(elements[i] + " ");
  16.         }
  17.         sb.append("\n");
  18.         return sb.toString();
  19.     }
  20.  
  21.     public void printElements() {
  22.         System.out.print("Elements: " + this.toString());
  23.     }
  24.  
  25.     @SuppressWarnings("unchecked")
  26.     public ResizableArray() {
  27.         elements = (T[]) new Object[20];
  28.         size = 0;
  29.         maxSize = 1;
  30.     }
  31.  
  32.     public void addElement(T element) {
  33.         if (size == maxSize) {
  34.             T[] temp = elements;
  35.             elements = (T[]) new Object[2 * maxSize];
  36.             IntStream.range(0, size).forEach(i -> elements[i] = temp[i]);
  37.             maxSize *= 2;
  38.         }
  39.         elements[size++] = element;
  40.     }
  41.  
  42.     public void avoidElement(int index) {
  43.         int elementsToMove = size - 1 - index;
  44.         if (elementsToMove > 0) {
  45.             /* This will copy the sub-array over the index element */
  46.             System.arraycopy(elements, index + 1, elements, index, elementsToMove);
  47.         }
  48.         elements[--size] = null;
  49.     }
  50.  
  51.     public boolean removeElement(T element) {
  52.         if (element != null) {
  53.             for (int i = 0; i < size; i++) {
  54.                 //System.out.println(elements[i] + " " + element);
  55.                 if (elements[i].equals(element)) {
  56.                     avoidElement(i);
  57.                     //printElements();
  58.                     return true;
  59.                 }
  60.             }
  61.         } else {
  62.             /*
  63.             return IntStream.range(0, size).filter(i->elements[i]==null).findAny().orElse(false);
  64.             */
  65.             IntStream.range(0, size)
  66.                     .filter(i -> elements[i] == null)
  67.                     .anyMatch(i -> {
  68.                         avoidElement(i);
  69.                         return true;
  70.                     });
  71.             /*
  72.             for (int i = 0; i < size; i++) {
  73.                 if (elements[i] == null) {
  74.                     avoidElement(i);
  75.                     return true;
  76.                 }
  77.             }
  78.             */
  79.         }
  80.         return false;
  81.     }
  82.  
  83.     public boolean contains(T element) {
  84.         return IntStream.range(0, size).anyMatch(i -> elements[i].equals(element));
  85.         /*
  86.         for (int i = 0; i < size; i++) {
  87.             //System.out.println("Index: " + i + " elements[i]: " + elements[i] + "  element: " + element);
  88.             if (elements[i].equals(element)) {
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.         */
  94.     }
  95.  
  96.     public Object[] toArray() {
  97.         return Arrays.copyOf(elements, size);
  98.     }
  99.  
  100.     public boolean isEmpty() {
  101.         return size == 0;
  102.     }
  103.  
  104.     public int count() {
  105.         return size;
  106.     }
  107.  
  108.     public T elementAt(int index) throws ArrayIndexOutOfBoundsException {
  109.         if (index < 0 || index > count()) {
  110.             throw new ArrayIndexOutOfBoundsException();
  111.         } else {
  112.             return elements[index];
  113.         }
  114.     }
  115.  
  116.     public static <T> void copyAll(ResizableArray<? super T> dest, ResizableArray<? extends T> src) {
  117.         int len = src.count();
  118.         IntStream.range(0, len).forEach(i -> dest.addElement(src.elementAt(i)));
  119.     }
  120. }
  121.  
  122. class IntegerArray extends ResizableArray<Integer> {
  123.  
  124.     public IntegerArray() {
  125.     }
  126.  
  127.     public double sum() {
  128.         double sum = 0;
  129.         for (int i = 0; i < count(); i++) {
  130.             sum += (Integer) elementAt(i);
  131.         }
  132.         return sum;
  133.     }
  134.  
  135.     public double mean() {
  136.         return sum() / count();
  137.     }
  138.  
  139.     public int countNonZero() {
  140.         int count = 0;
  141.         for (int i = 0; i < count(); i++) {
  142.             if ((Integer) elementAt(i) != 0) {
  143.                 count++;
  144.             }
  145.         }
  146.         return count;
  147.     }
  148.  
  149.     public IntegerArray distinct() {
  150.         IntegerArray result = new IntegerArray();
  151.         for (int i = 0; i < this.count(); i++) {
  152.             boolean alreadyHaveIt = false;
  153.             for (int j = 0; j < result.count(); j++) {
  154.                 if (this.elementAt(i) == result.elementAt(j)) {
  155.                     alreadyHaveIt = true;
  156.                     break;
  157.                 }
  158.             }
  159.             if (!alreadyHaveIt) {
  160.                 result.addElement(this.elementAt(i));
  161.             }
  162.         }
  163.         return result;
  164.     }
  165.  
  166.     public IntegerArray increment(int offset) {
  167.         IntegerArray result = new IntegerArray();
  168.         for (int i = 0; i < count(); i++) {
  169.             int current = elementAt(i).intValue();
  170.             result.addElement(current + offset);
  171.         }
  172.         return result;
  173.     }
  174. }
  175.  
  176. public class ResizableArrayTest {
  177.  
  178.     public static void main(String[] args) {
  179.         Scanner jin = new Scanner(System.in);
  180.         int test = jin.nextInt();
  181.         if (test == 0) { //test ResizableArray on ints
  182.             ResizableArray<Integer> a = new ResizableArray<Integer>();
  183.             System.out.println(a.count());
  184.             int first = jin.nextInt();
  185.             a.addElement(first);
  186.             System.out.println(a.count());
  187.             int last = first;
  188.             while (jin.hasNextInt()) {
  189.                 last = jin.nextInt();
  190.                 a.addElement(last);
  191.             }
  192.             System.out.println(a.count());
  193.             System.out.println(a.contains(first));
  194.             System.out.println(a.contains(last));
  195.             System.out.println(a.removeElement(first));
  196.             System.out.println(a.contains(first));
  197.             System.out.println(a.count());
  198.         }
  199.         if (test == 1) { //test ResizableArray on strings
  200.             ResizableArray<String> a = new ResizableArray<String>();
  201.             System.out.println(a.count());
  202.             String first = jin.next();
  203.             a.addElement(first);
  204.             System.out.println(a.count());
  205.             String last = first;
  206.             for (int i = 0; i < 4; ++i) {
  207.                 last = jin.next();
  208.                 a.addElement(last);
  209.             }
  210.             System.out.println(a.count());
  211.             System.out.println(a.contains(first));
  212.             System.out.println(a.contains(last));
  213.             System.out.println(a.removeElement(first));
  214.             System.out.println(a.contains(first));
  215.             System.out.println(a.count());
  216.             ResizableArray<String> b = new ResizableArray<String>();
  217.             ResizableArray.copyAll(b, a);
  218.             System.out.println(b.count());
  219.             System.out.println(a.count());
  220.             System.out.println(a.contains(first));
  221.             System.out.println(a.contains(last));
  222.             System.out.println(b.contains(first));
  223.             System.out.println(b.contains(last));
  224.             ResizableArray.copyAll(b, a);
  225.             System.out.println(b.count());
  226.             System.out.println(a.count());
  227.             System.out.println(a.contains(first));
  228.             System.out.println(a.contains(last));
  229.             System.out.println(b.contains(first));
  230.             System.out.println(b.contains(last));
  231.             System.out.println(b.removeElement(first));
  232.             System.out.println(b.contains(first));
  233.             System.out.println(b.removeElement(first));
  234.             System.out.println(b.contains(first));
  235.  
  236.             System.out.println(a.removeElement(first));
  237.             ResizableArray.copyAll(b, a);
  238.             System.out.println(b.count());
  239.             System.out.println(a.count());
  240.             System.out.println(a.contains(first));
  241.             System.out.println(a.contains(last));
  242.             System.out.println(b.contains(first));
  243.             System.out.println(b.contains(last));
  244.         }
  245.         if (test == 2) { //test IntegerArray
  246.             IntegerArray a = new IntegerArray();
  247.             System.out.println(a.isEmpty());
  248.             while (jin.hasNextInt()) {
  249.                 a.addElement(jin.nextInt());
  250.             }
  251.             jin.next();
  252.             System.out.println(a.sum());
  253.             System.out.println(a.mean());
  254.             System.out.println(a.countNonZero());
  255.             System.out.println(a.count());
  256.             IntegerArray b = a.distinct();
  257.             System.out.println(b.sum());
  258.             IntegerArray c = a.increment(5);
  259.             System.out.println(c.sum());
  260.             if (a.sum() > 100)
  261.                 ResizableArray.copyAll(a, a);
  262.             else
  263.                 ResizableArray.copyAll(a, b);
  264.             System.out.println(a.sum());
  265.             System.out.println(a.removeElement(jin.nextInt()));
  266.             System.out.println(a.sum());
  267.             System.out.println(a.removeElement(jin.nextInt()));
  268.             System.out.println(a.sum());
  269.             System.out.println(a.removeElement(jin.nextInt()));
  270.             System.out.println(a.sum());
  271.             System.out.println(a.contains(jin.nextInt()));
  272.             System.out.println(a.contains(jin.nextInt()));
  273.         }
  274.         if (test == 3) { //test insanely large arrays
  275.             LinkedList<ResizableArray<Integer>> resizable_arrays = new LinkedList<ResizableArray<Integer>>();
  276.             for (int w = 0; w < 500; ++w) {
  277.                 ResizableArray<Integer> a = new ResizableArray<Integer>();
  278.                 int k = 2000;
  279.                 int t = 1000;
  280.                 for (int i = 0; i < k; ++i) {
  281.                     a.addElement(i);
  282.                 }
  283.  
  284.                 a.removeElement(0);
  285.                 for (int i = 0; i < t; ++i) {
  286.                     a.removeElement(k - i - 1);
  287.                 }
  288.                 resizable_arrays.add(a);
  289.             }
  290.             System.out.println("You implementation finished in less then 3 seconds, well done!");
  291.         }
  292.     }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement