Convert class to generic

Asked

Viewed 127 times

0

I need to convert the code class below into a generic implementation using a 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()); 

    } 

}
  • Has more than one form, in addition to replacing the type "int" with the generic type?

2 answers

0

A possible solution would be the following:

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 Arraylist is a dynamic vector (positions are added to it as per demand, and it is not necessary to define its size previously).

Thus, adding items to the stack is much easier, no longer being necessary to check if it exceeded the maximum size:

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

The Array type definition is the main point: declaring it as an Object vector, we allow objects of any type to be added because all classes extend Object, I mean, they’re your daughters.

private ArrayList <Object> stack;

0

A solution would be parameterize its Arraystack class, and so you avoid cast the 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());
      } 
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.