View Single Post
  #2 (permalink)  
Old 03-25-2008, 07:06 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Check the Stack class api in the javadocs to learn what methods are available.
Code:
import java.util.*; public class StackTest { public static void main(String[] args) { String[] names = { "Alice", "Sam", "John", "Mary", "Nancy", "Fred" }; String searchFor = "John"; Stack<String> stack = new Stack<String>(); for(int i = 0; i < names.length; i++) stack.push(names[i]); System.out.println("stack = " + stack); // Look for searchFor string in stack: String[] stackItems = new String[stack.size()]; int size = stack.size(); for(int i = 0; i < size; i++) { String s = stack.pop(); if(s.equals(searchFor)) { System.out.println("Found " + searchFor + " at index of " + i + " in stack."); break; } stackItems[i] = s; } System.out.println(Arrays.toString(stackItems)); } }
Reply With Quote