Friday 26 June 2015

Java : Collection Framework : Stack search


Click here to watch in Youtube :
https://www.youtube.com/watch?v=1AujmHdFgcQ&list=UUhwKlOVR041tngjerWxVccw

StackExample.java
import java.util.Stack;

/*
 Method 

 public int search(Object o)

 Parameters:
 
 o - the desired object.

 Returns:
 
 the 1-based position from the top of the stack where the object is located; 
 the return value -1 indicates that the object is not on the stack.

 */

public class StackExample
{

    public static void main(String[] args)
    {
        Stack<String> stack = new Stack<String>();
    
        stack.push("Apple");
        stack.push("Ball");
        stack.push("Cat");
        stack.push("Dog");

        System.out.println("stack : " + stack + "\n");

        /*
         * Returns the 1-based position where an object is on this stack. If the
         * object o occurs as an item in this stack, this method returns the
         * distance from the top of the stack of the occurrence nearest the top
         * of the stack; the topmost item on the stack is considered to be at
         * distance 1. The equals method is used to compare o to the items in
         * this stack.
         * 
         * Stack [ 
         * Dog   - 1 
         * Cat   - 2 
         * Ball  - 3 
         * Apple - 4 ]
         */
        int position = stack.search("Cat"); 
        System.out.println("position of 'Cat' : " + position + "\n");
        
        
        position = stack.search("Ball"); 
        System.out.println("position of 'Ball' : " + position + "\n");
        
    }

}
Output
stack : [Apple, Ball, Cat, Dog]

position of 'Cat' : 2

position of 'Ball' : 3

To Download StackDemoSearch Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/StackDemoSearch.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment