集册 Java实例教程 堆栈泛型类声明

堆栈泛型类声明

欢马劈雪     最近更新时间:2020-01-02 10:19:05

557
堆栈泛型类声明
/*来自 时 代 J a v a - N o w J a v a . c o m*/

import java.util.ArrayList;


class EmptyStackException extends RuntimeException

{

   // no-argument constructor

   public EmptyStackException()

   {

      this("Stack is empty");

   } 


   // one-argument constructor

   public EmptyStackException(String message)

   {

      super(message);

   }/* 来自 时 代 J a v a - N o w J a v a . c o m*/

}

class Stack<T>  

{

   private final ArrayList<T> elements; // ArrayList stores stack elements


   // no-argument constructor creates a stack of the default size

   public Stack()

   {

      this(10); // default stack size

   } 


   // constructor creates a stack of the specified number of elements

   public Stack(int capacity)

   {

      int initCapacity = capacity > 0 ? capacity : 10; // validate

      elements = new ArrayList<T>(initCapacity); // create ArrayList

   }


   // push element onto stack

   public void push(T pushValue)

   {

      elements.add(pushValue); // place pushValue on Stack

   } 


   // return the top element if not empty; else throw EmptyStackException

   public T pop()

   {

      if (elements.isEmpty()) // if stack is empty

         throw new EmptyStackException("Stack is empty, cannot pop");


      // remove and return top element of Stack

      return elements.remove(elements.size() - 1); 

   }

}


public class Main 

{

   public static void main(String[] args) 

   {

      double[] doubleElements = {1.1, 2.2, 3.3, 4.4, 5.5};

      int[] integerElements = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

      

      // Create a Stack<Double> and a Stack<Integer>

      Stack<Double> doubleStack = new Stack<>(5); 

      Stack<Integer> integerStack = new Stack<>(); 


      // push elements of doubleElements onto doubleStack

      testPushDouble(doubleStack, doubleElements); 

      testPopDouble(doubleStack); // pop from doubleStack


      // push elements of integerElements onto integerStack

      testPushInteger(integerStack, integerElements); 

      testPopInteger(integerStack); // pop from integerStack

   } 


   // test push method with double stack

   private static void testPushDouble(Stack<Double> stack, double[] values)

   {

      System.out.printf("%nPushing elements onto doubleStack%n");


      // push elements to Stack

      for (double value : values)

      {

         System.out.printf("%.1f ", value);

         stack.push(value); // push onto doubleStack

      }

   }


   // test pop method with double stack

   private static void testPopDouble(Stack<Double> stack)

   {

      // pop elements from stack

      try

      {

         System.out.printf("%nPopping elements from doubleStack%n");

         double popValue; // store element removed from stack


         // remove all elements from Stack

         while (true)

         {

            popValue = stack.pop(); // pop from doubleStack

            System.out.printf("%.1f ", popValue); 

         }

      }

      catch(EmptyStackException emptyStackException)

      {

         System.err.println();

         emptyStackException.printStackTrace();

      } 

   } 


   // test push method with integer stack

   private static void testPushInteger(

      Stack<Integer> stack, int[] values)

   {

      System.out.printf("%nPushing elements onto integerStack%n");


      // push elements to Stack

      for (int value : values)

      {

         System.out.printf("%d ", value);

         stack.push(value); // push onto integerStack

      } 
展开阅读全文