Question: Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding
Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding a new element to the stack. If the array is full, create a new array that doubles the current array size and copy the elements from the current array to the new array.
Listing

1 public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList (); 3 public int getSize() { return list.size(); 4. 5 6. public E peek() { return list.get (getSize() - 1); 10 11 public void push(E o) { list.add(o); 12 13 14 15 16 17 18 19 20 public E pop() { E o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; 21 22 public boolean isEmpty() { return list.isEmpty(); 23 24 25 @0verride public String toString() { return "stack: 26 27 + list.toString(); 28 29 30 }
Step by Step Solution
3.47 Rating (173 Votes )
There are 3 Steps involved in it
Program Plan Create a class named GenericStck that implements the stack operations using an array of objects o Implement a default constructor creates ... View full answer
Get step-by-step solutions from verified subject matter experts
