Advertisement
Guest User

ReversedList<T>

a guest
Jan 9th, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package _06ReversedList;
  2.  
  3. import java.util.Iterator;
  4.  
  5. /**
  6.  * Created by IntelliJ IDEA.
  7.  * User: LAPD
  8.  * Date: 26.12.2018 г.
  9.  * Time: 16:01 ч.
  10.  */
  11. public class ReversedList<T> implements Iterable<T> {
  12.  
  13.     private static int INITIAL_CAPACITY = 2;
  14.  
  15.     private T[] elements;
  16.  
  17.     private int count;
  18.  
  19.     public ReversedList(int capacity) {
  20.         INITIAL_CAPACITY = capacity;
  21.         new ReversedList();
  22.     }
  23.  
  24.     public ReversedList() {
  25.         count = 0;
  26.         elements = (T[]) new Object[INITIAL_CAPACITY];
  27.     }
  28.  
  29.     public int count() {
  30.         return this.count;
  31.     }
  32.  
  33.     public int capacity() {
  34.         return INITIAL_CAPACITY;
  35.     }
  36.  
  37.     public T get(int index) {
  38.  
  39.         checkIndex(index);
  40.  
  41.         return elements[count - 1 - index];
  42.     }
  43.  
  44.     public void set(int index, T item) {
  45.  
  46.         checkIndex(index);
  47.  
  48.         this.elements[count - 1 - index] = item;
  49.     }
  50.  
  51.     public void add(T element) {
  52.  
  53.         if (count >= INITIAL_CAPACITY) {
  54.             resize();
  55.         }
  56.  
  57.         this.elements[this.count++] = element;
  58.     }
  59.  
  60.     public T removeAt(int index) {
  61.  
  62.         checkIndex(index);
  63.  
  64.         index = count - 1 - index;
  65.  
  66.         T element = this.elements[index];
  67.  
  68.         shift(index);
  69.  
  70.         this.count--;
  71.  
  72.         if (this.count <= INITIAL_CAPACITY / 4
  73.                 && INITIAL_CAPACITY > 2) {
  74.             resize();
  75.         }
  76.  
  77.         return element;
  78.     }
  79.  
  80.     private void checkIndex(int index) {
  81.         if (index < 0 || index >= count) {
  82.             throw new IllegalArgumentException();
  83.         }
  84.     }
  85.  
  86.     private void resize() {
  87.  
  88.         if (count <= INITIAL_CAPACITY / 4) {
  89.             INITIAL_CAPACITY /= 2;
  90.         } else if (count >= INITIAL_CAPACITY) {
  91.             INITIAL_CAPACITY *= 2;
  92.         }
  93.  
  94.         T[] newArray = (T[]) new Object[INITIAL_CAPACITY];
  95.         System.arraycopy(elements, 0,
  96.                 newArray, 0, count);
  97.  
  98.         this.elements = newArray;
  99.     }
  100.  
  101.     private void shift(int index) {
  102.  
  103.         System.arraycopy(elements, index + 1,
  104.                 elements, index, count - 1 - index);
  105.  
  106.         elements[count - 1] = null;
  107.     }
  108.  
  109.  
  110.     @Override
  111.     public Iterator<T> iterator() {
  112.         return new ListIterator();
  113.     }
  114.  
  115.     private final class ListIterator implements Iterator<T> {
  116.         private int counter;
  117.  
  118.         public ListIterator() {
  119.             this.counter = 0;
  120.         }
  121.  
  122.         @Override
  123.         public boolean hasNext() {
  124.             return count > this.counter;
  125.         }
  126.  
  127.         @Override
  128.         public T next() {
  129.             return get(this.counter++);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement