Generic class Stack : Generic Class « Generics « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Generics » Generic Class 
12.6.4.Generic class Stack
class Stack<E> {
  private final int size;

  private int top;

  private E[] elements;

  public Stack() {
    this(10);
  }

  public Stack(int s) {
    size = s > ? s : 10;
    top = -1;

    elements = (E[]) new Object[size]// create array
  }

  public void push(E pushValue) {
    if (top == size - 1// if stack is full
      throw new FullStackException(String.format("Stack is full, cannot push %s", pushValue));

    elements[++top= pushValue; // place pushValue on Stack
  }

  public E pop() {
    if (top == -1// if stack is empty
      throw new EmptyStackException("Stack is empty, cannot pop");

    return elements[top--]// remove and return top element of Stack
  }
}

class EmptyStackException extends RuntimeException {
  public EmptyStackException() {
    this("Stack is empty");
  }

  public EmptyStackException(String exception) {
    super(exception);
  }
}

class FullStackException extends RuntimeException {
  public FullStackException() {
    this("Stack is full");
  }

  public FullStackException(String exception) {
    super(exception);
  }
}

public class MainClass {

  public static void main(String args[]) {
    double[] doubleElements = 1.12.23.34.45.56.6 };
    int[] integerElements = 1234567891011 };

    Stack<Double> doubleStack = new Stack<Double>(5);
    Stack<Integer> integerStack = new Stack<Integer>(10);

    // test Push Double
    try {
      System.out.println("\nPushing elements onto doubleStack");

      for (double element : doubleElements) {
        System.out.printf("%.1f ", element);
        doubleStack.push(element);
      }
    catch (FullStackException fullStackException) {
      System.err.println();
      fullStackException.printStackTrace();
    }

    // test Pop Double

    try {
      System.out.println("\nPopping elements from doubleStack");
      double popValue;

      while (true) {
        popValue = doubleStack.pop()// pop from doubleStack
        System.out.printf("%.1f ", popValue);
      }
    catch (EmptyStackException emptyStackException) {
      System.err.println();
      emptyStackException.printStackTrace();
    }

    // test push method with integer stack
    try {
      System.out.println("\nPushing elements onto integerStack");

      for (int element : integerElements) {
        System.out.printf("%d ", element);
        integerStack.push(element);
      }
    catch (FullStackException fullStackException) {
      System.err.println();
      fullStackException.printStackTrace();
    }
    // test pop method with integer stack
    try {
      System.out.println("\nPopping elements from integerStack");
      int popValue; // store element removed from stack

      // remove all elements from Stack
      while (true) {
        popValue = integerStack.pop();
        System.out.printf("%d ", popValue);
      }
    catch (EmptyStackException emptyStackException) {
      System.err.println();
      emptyStackException.printStackTrace();
    }

  }
}
12.6.Generic Class
12.6.1.Defining a Generic Class Type
12.6.2.Using Primitive Type Wrapper Class Types as Arguments
12.6.3.The Run-Time Types of Generic Type Instances
12.6.4.Generic class Stack
12.6.5.Use generic method to test generic Stack
12.6.6.Raw type test for a generic Stack
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.