Click here to watch in Youtube :
https://www.youtube.com/watch?v=kkRDmV0dIDs&list=UUhwKlOVR041tngjerWxVccw
StackExample.java
Output
https://www.youtube.com/watch?v=kkRDmV0dIDs&list=UUhwKlOVR041tngjerWxVccw
StackExample.java
import java.util.Stack; /* Method public E peek() Returns: the object at the top of this stack (the last item of the Vector object). Throws: EmptyStackException - if this stack is empty. */ 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"); /* * [ * Dog * Cat * Ball * Apple * ] */ System.out.println("Before peek() method is called : stack : " + stack + "\n"); /* * Looks at the object at the top of this stack without removing it from * the stack. */ String object = stack.peek(); System.out.println("object : " + object + "\n"); System.out.println("After peek() method is called : stack : " + stack); } }
Before peek() method is called : stack : [Apple, Ball, Cat, Dog] object : Dog After peek() method is called : stack : [Apple, Ball, Cat, Dog]
https://sites.google.com/site/javaee4321/java-collections/StackDemoPeekApp.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment