title: Generic stack implementation
body: <p>I'm trying to implement a generic stack. </p>
  
  <p>Here's the interface</p>
  
  <pre><code>package stack;
  
  public interface Stack&lt;T&gt;{
      void push(T number);
      T pop();
      T peek();
      boolean isEmpty();
      boolean isFull();
  }
  </code></pre>
  
  <p>Here's the class</p>
  
  <pre><code>package stack;
  
  import java.lang.reflect.Array;
  import java.util.EmptyStackException;
  
  public class StackArray &lt;T&gt; implements Stack&lt;T&gt;{
      private int maxSize;
      private T[] array;
      private int top;
  
      public StackArray(int maxSize) {
          this.maxSize = maxSize;
  //        @SuppressWarnings("unchecked")
          this.array = (T[]) Array.newInstance(StackArray.class, maxSize);
          this.top = -1;
      }
  
      private T[] resizeArray() {
          /**
           * create a new array double the size of the old, copy the old elements then return the new array */
          int newSize = maxSize * 2;
          T[] newArray = (T[]) Array.newInstance(StackArray.class, newSize);
          for(int i = 0; i &lt; maxSize; i++) {
              newArray[i] = this.array[i];
          }
          return newArray;
      }
  
      public boolean isEmpty() {
          return top == -1;
      }
  
      public boolean isFull() {
          return top == maxSize-1;
      }
  
      public void push(T element) {
          if(!this.isFull()) {
              ++top;
              array[top] = element;
          }
          else {
              this.array = resizeArray();
              array[++top] = element;
          }
      }
  
      public T pop() {
          if(!this.isEmpty())
              return array[top--];
          else {
              throw new EmptyStackException();
          }
      }
  
      public T peek() {
          return array[top];
      }
  }
  </code></pre>
  
  <p>Here's the Main class</p>
  
  <pre><code>package stack;
  
  
  public class Main {
      public static void main(String[] args) {
          String word = "Hello World!";
          Stack &lt;Character&gt;stack = new StackArray&lt;&gt;(word.length());
  
  //        for(Character ch : word.toCharArray()) {
  //            stack.push(ch);
  //        }
  
          for(int i = 0; i &lt; word.length(); i++) {
              stack.push(word.toCharArray()[i]);
          }
  
          String reversedWord = "";
          while(!stack.isEmpty()) {
              char ch = (char) stack.pop();
              reversedWord += ch;
          }
          System.out.println(reversedWord);
  
      }
  }
  </code></pre>
  
  <p>The error is</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayStoreException: java.lang.Character
      at stack.StackArray.push(StackArray.java:40)
      at stack.Main.main(Main.java:14)
  </code></pre>
  
  <p>line 40 is in the push method</p>
  
  <pre><code>        array[top] = element;
  </code></pre>
  
  <p><em>Side Question:
  Any way to suppress the warning in the constructor? :)</em></p>