Advertisement
Guest User

Untitled

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