Share on Google+Share on Google+

Naulej kumar
Java collection Stack example
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners

How to use Stack class in java collection?

Ads

View Answers

June 22, 2012 at 3:00 PM


The Stack class uses the First In Last Out(FILO) mechanism. Here is an example of Stack class.

import java.util.Stack;

public class StackExample {
    public static void main(String [] args){
        Stack stack = new Stack();
        stack.push("Angelina Jolie");
        stack.push("Jennifer Aniston");
        stack.push("Scarlett Johansson");
        stack.push("Reese Witherspoon");
        stack.push("Megan Fox");
        stack.push("Nicole Kidman");
        System.out.println("The elements in the Stack :- " + stack);
        System.out.println("The element at the top :- " +stack.peek());
        System.out.println("The element poped out of the stack :- " + stack.pop());
        System.out.println("The element in a stack after pop out an element- : " +stack);
        System.out.println("The result of searching :- " +stack.search(" r u"));
    }


}

Output:-

The elements in the Stack :- [Angelina Jolie, Jennifer Aniston, Scarlett Johansson, Reese Witherspoon, Megan Fox, Nicole Kidman]
The element at the top :- Nicole Kidman
The element poped out of the stack :- Nicole Kidman
The element in a stack after pop out an element- : [Angelina Jolie, Jennifer Aniston, Scarlett Johansson, Reese Witherspoon, Megan Fox]
The result of searching :- -1

Description:- The above example demonstrates you the Stack class in java collection. We are using three methods of Stack. push(object items): The push method add an items to the top of the Stack. peek(): The peek() method returns the element at the top of the Stack, and pop(): The pop() method remove the element at the top of the Stack and returns the element.










Related Tutorials/Questions & Answers:
Java collection Stack example
Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism... :- -1 Description:- The above example demonstrates you the Stack class in java
Collection : Stack Example
Collection : Stack Example In this tutorial we will describe implementation of stack with example. Stack : Stack is based on concept of LIFO(Last In First... if stack is empty. Some other methods - empty(), peek(), search(Object o) Example
Advertisements
Java Stack Example
the stack collection in the Java programming. In this example you will see how...Java Stack Example In this section we will read about how you can provide... Collection. Stack may have the two states while pushing and popping is occurred
Java Stack Example
Java Stack Example In this section you will learn about Stack class  in java, how to use Stack in java. Before going into details we should know what... the item in the stack and how far it is from top of stack Example : A Java code
Java collection Queue Example
Java collection Queue Example  How can we use Queue in java collection?   import java.util.LinkedList; import java.util.Queue; public... LinkedList(); queue.add("Java"); queue.add("DotNet
Java Collection iterator with example
. Example of Java Collection Iterator import java.util.ArrayList; import... The Java Collection Iterator is present at the highest level interface in the Collection framework. Iterator interface has methods for traversing
Example of Java Stack Program
Example of Java Stack Program     ... it. Here in the example describes the methods to prepare an example of Java stack... of this example is given below: The contents of Stack is[10, a] The size of an Stack
stack in java/
stack in java/  java code for stack push pop and display operation? thanks
stack and queue - Java Beginners
stack and queue  write two different program in java 1.) stack 2...://www.roseindia.net/java/example/java/util/StackImplement.shtml http://www.roseindia.net/java/example/java/util/QueueImplement.shtml Hope
java technologies stack
java technologies stack  java technologies stack
stack - Java Beginners
on Stack visit to : Thanks   http://www.roseindia.net/java/example/java/util/ Thanks  I think there is bit wrong in the above answer... that inputs a line of text and uses a stack object to print the words of the line
Implementing a Stack in Java
Implementing a Stack in Java     ... in Java. A Stack is like a bucket in which you can put elements one-by-one... entered element. Stack is a collection of data and follows the LIFO (Last
Java Stack - Java Beginners
Java Stack  Can you give me a code using Java String STACK using the parenthesis symbol ( ) the user will be the one to input parenthesis......) if Open parenthesis push it to stack otherwise Pop the stack Thank you
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
Java Collection
Java Collection  What is the Collection interface
Java stack help
Java stack help  How would I use stacks in java to do a calculation like: 4*((3+3)/(5-3
Generic Java Stack
Generic Java Stack  Hi here the question the interviewer asked me- package jp.co.worksap.intern; /** *The Stack class represents a last-in-first-out(LIFO) stack of objects *And this class can look at the object
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection
ADT stack - Java Beginners
ADT stack  Design a Java interface for ADT Stack. Develop two....  Hi Friend, Try the following codes: ADT Stack: import...("Stack is empty"); return; } else { String str = " "; for (int i = 0; i <= top
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
Java Collection : NavigableSet Example
Java Collection : NavigableSet Example In this tutorial we are describing NavigableSet of collection Framework NavigableSet : NavigableSet interface..., boolean inclusive). Example : Here is an example of NavigableSet . package
ADT stack - Java Beginners
]. 2)Design a Java interface for ADT Stack. Develop two different classes...ADT stack  I want a algorithm for the below programs.... 1)Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons
Collection Iterate Example
Collection Iterate Example     ... of interface Iterator in Java. The description of the code is given below... the hasNext() method through the following java program. True is return
Collection : LinkedList Example
This tutorial contains description of Collection LinkedList with example
Collection : ArrayList Example
This tutorial contains description of Collection ArrayList with example
Collection in java
Collection in java  What are the meaning of the letters E,T,K and V that come in the collection concept
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java? whats the java program or code to that. Thank you rose india and java... the number: "); int num = input.nextInt(); Stack stack = new Stack
Java Stack
Java Stack       The Stack class works on the principle last-in-first-out (LIFO) stack of objects... java stack click on the link: http:/www.roseindia.net/java/beginners/stack
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When... on due to which stack's limit exceeds. For example, take a look at given
Java Collection : LinkedHashSet Example
Java Collection : LinkedHashSet Example This tutorial contains description of LinkedHashSet with example. LinkedHashSet : LinkedHashSet class is defined... of HashSet. Example : package collection; import java.util.LinkedHashSet; class
Java Collection : TreeSet Example
Java Collection : TreeSet Example This tutorial contains description of  TreeSet with example. TreeSet  : TreeSet class is defined in java.util... constructor - TreeSet(), TreeSet(Collection c), TreeSet(Comparator c
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads