Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java collection Stack example
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


How to use Stack class in java collection?


Advertisement
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
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
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
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
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 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 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 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
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
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
 

 

 

DMCA.com