Convert class to generic

0

I need to convert the code class below into a generic implementation using an ArrayList .

Can anyone suggest a better way to do this?

Code:

public class ArrayStack { 

    private int maxsize; Retirar essas 2 linhas…
    private int top; 
    private int[] items; 

    public ArrayStack(int maxsize) { 
    if (maxsize <= 0) 
                     throw new ArrayStackException( 
    "Stack size must be positive"); 
    items = new int[maxsize]; 
    this.maxsize = maxsize; 
    top = 0; 
    } 

    public void push(int item) { 
    if (top == items.length) 
        throw new ArrayStackException("Overflow Error"); 
    items[top] = item; 
    top++; 
    } 

    public int pop() { 
    if (isEmpty()) 
        throw new ArrayStackException("Underflow Error"); 
    return items[--top]; 
    } 

    public boolean isEmpty() { 
     return (top == 0); 
    } 

    public static class ArrayStackException extends RuntimeException { 
     public ArrayStackException(String message) { 
         super(message); 
     } 
    } 
    public static void main(String[] args) { 
    ArrayStack stack = new ArrayStack(3); 
    stack.push(1); 
    stack.push(2); 
    stack.push(3); 
    //stack.push(4); //overflow error 
    System.out.println(stack.pop()); 
    System.out.println(stack.pop()); 
    System.out.println(stack.pop()); 

    } 

}
    
asked by anonymous 19.04.2018 / 01:57

2 answers

0

One possible solution would be as follows:

public class ArrayStack { 
    private ArrayList <Object> stack;


    public ArrayStack() { 
        stack = new ArrayList <> ();
    } 

    public void push(Object item) { 
        stack.add(item);
    } 

    public Object pop() {
        if (!isEmpty()) {
                return stack.remove(stack.size() - 1);
        }
        throw new ArrayStackException("Underflow Error"); 
    } 

    public boolean isEmpty() { 
        return (stack.isEmpty());
    } 

    public static class ArrayStackException extends RuntimeException { 
        public ArrayStackException(String message) { 
            super(message); 
        } 
    } 

    public static void main(String[] args) { 
        ArrayStack stack = new ArrayStack(); 

        stack.push(1); 
        stack.push(2); 
        stack.push(3); 

        System.out.println(stack.pop()); 
        System.out.println(stack.pop()); 
        System.out.println(stack.pop());
    } 
}

With the use of an ArrayList, several implementations present in the original code become unnecessary, because the ArrayList is a dynamic vector (positions are added to it according to the demand, and it is not necessary to define its size previously). >

So adding items to the stack is a lot easier, so it's no longer necessary to check if the maximum size has been exceeded:

public void push(Object item) { 
    stack.add(item);
} 

Array type definition is the main point: declaring it as an Object vector, we allow objects of any type to be added, since all classes extend Object , that is, they are their daughters.

private ArrayList <Object> stack;
    
21.04.2018 / 02:07
0

One solution would be to parameterize your ArrayStack class , so you avoid casting object in main.

public class ArrayStack <E> { 
      private List <E> stack;

      public ArrayStack() { 
          this.stack = new ArrayList<E>();
      } 

      public void push(E item) { 
          stack.add(item);
      } 

      public E pop() {
          if (!isEmpty()) {
                  return stack.remove(stack.size() - 1);
          }
          throw new ArrayStackException("Underflow Error"); 
      } 

      public boolean isEmpty() { 
          return (stack.isEmpty());
      } 

      public static class ArrayStackException extends RuntimeException { 
          public ArrayStackException(String message) { 
              super(message); 
          } 
      } 

      public static void main(String[] args) { 
          ArrayStack<String> stack = new ArrayStack<String>(); 

          stack.push("hello"); 
          stack.push("world"); 

          System.out.println(stack.pop()); 
          System.out.println(stack.pop()); 
          System.out.println(stack.pop());
      } 
  }
    
18.10.2018 / 20:55