package structures.stack;

import java.util.EmptyStackException;

import static java.util.Objects.requireNonNull;

public class ArrayStack<E> implements Stack<E> {

    private int capacity = 2;
    private E[] elements;
    private int size = 0;

    @SuppressWarnings("unchecked")
    public ArrayStack() {
        this.elements = (E[]) new Object[this.capacity];
    }

    @SuppressWarnings("unchecked")
    public ArrayStack(int initialCapacity) {
        if (initialCapacity < 1) {
            throw new IllegalArgumentException("Initial capacity must be greater than 0");
        }

        this.capacity = initialCapacity;
        this.elements = (E[]) new Object[this.capacity];
    }

    @Override
    public E push(E element) {
        requireNonNull(element, "Null elements cannot be pushed onto a stack");

        if (isFull()) {
            expand();
        }

        elements[size++] = element;

        return element;
    }

    @Override
    public E pop() {
        assertNotEmpty();

        return elements[--size];
    }

    @Override
    public E peek() {
        assertNotEmpty();

        return elements[size - 1];
    }

    @Override
    public boolean isEmpty() {
        return size <= 0;
    }

    @Override
    public int size() {
        return this.size;
    }

    private void assertNotEmpty() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
    }

    private boolean isFull() {
        return size >= elements.length;
    }

    @SuppressWarnings("unchecked")
    private void expand() {
        capacity = capacity * 2;
        E[] temporary = (E[]) new Object[capacity];
        System.arraycopy(this.elements, 0, temporary, 0, elements.length);
        this.elements = temporary;
    }
}

@@ -162,38 +162,8 @@
  %7B%0A%0A
-    private int capacity = 2;%0A
     
@@ -184,16 +184,16 @@
 ements;%0A
+
     priv
@@ -309,37 +309,25 @@
  new Object%5B
-this.capacity
+2
 %5D;%0A    %7D%0A%0A  
@@ -545,49 +545,8 @@
  %7D%0A%0A
-        this.capacity = initialCapacity;%0A
     
@@ -586,14 +586,16 @@
 ect%5B
-this.c
+initialC
 apac
@@ -1486,16 +1486,20 @@
         
+int 
 capacity
@@ -1501,24 +1501,31 @@
 acity = 
-capacity
+elements.length
  * 2;%0A  
@@ -1599,21 +1599,16 @@
 raycopy(
-this.
 elements
@@ -1644,37 +1644,32 @@
 ength);%0A        
-this.
 elements = tempo

Remove unnecessary instance variable