search(Object o) 方法用于返回对象在此堆栈上的基于 1 的位置。示例import java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Searching 'code' in stack: "+st.search("code")); } }输出Searching 'code' in stack:
java.util.ArrayList 类的 get(int index) 方法返回此列表中指定位置处的元素。示例import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval=arrlist.get(3); System.out.println("Retrieved element is = " + retval); } }输出Number = 15 Number = 22 Number = 30 Number = 40 Retrieved element is = 40
peek() 方法用于查看此堆栈顶部的对象,而无需将其从堆栈中移除。示例import java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Top object is: "+st.peek()); } }输出Top object is: code