Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Use the Stack class in Java

import java.util.Stack; public class Main {   public static void main(String[] args) {     Stack<Integer> stack = new Stack<Integer>();     for (int i = 0; i < 10; i++) {       stack.push(i);       System.out.print(i + " ");     }     System.out.println("");     int position = stack.search(3);     System.out.println("Search result position: " + position);     System.out.println("Stack top: " + stack.peek());     while (!stack.empty()) {       System.out.print(stack.pop() + " ");     }   } }