Java Generics Stack Example

Java Generics Stack Example

Java Generics Stack Example: The following program creates generics stacks storing all ints, doubles, chars and strings. Java 5 enhanced for loop is used to print the elements.

Two Java Generics Stack Examples are given on Java Generics Stack.

  1. First one is general stack storing any data types. The methods push, peek, pop, size etc. are used.
  2. The present one is generics stack storing similar or specific type of elements. Enhanced for loop (foreach) is used to iterate the elements.

It is advised to read the first stack program before this where you get acquainted with Stack, methods and constructors available etc.


Java Generics Stack Example
Output screen of Java Generics Stack

Four Java generics stack objects, st1, st2, st3 and st4 are created that stores only ints, strings, characters and doubles values. With enhanced for loop (some people call it as foreach), all the elements of stacks are printed. To illustrate an example, it1 values are printed with Iterator interface also.

Pass your comments and suggestions to improve the quality of this tutorial “Java Generics Stack”.

Leave a Reply

Your email address will not be published. Required fields are marked *