- 栈这个概念作为计算机里面最重要的概念之一吗,一直以来就是我们面试中常问的问题,在JAVA中有它的操作API,下面就是它的一些案例。
import java.util.ArrayList;
import java.util.Stack;
public class Test6 {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
stack.add("1");
stack.add("1");
stack.add("2");
stack.add("3");
stack.add("5");
stack.add("4");
for (String string : stack) {
if (string.equals("2")) {
stack.peek();
}
}
int search1 = stack.search("1");
int search4 = stack.search("4");
System.out.println(search1);
System.out.println(search4);
System.out.println("======================");
String peek = stack.peek();
System.out.println(peek);
System.out.println(stack);
boolean empty = stack.isEmpty();
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1212");
boolean addAll = stack.addAll(arrayList);
System.out.println(addAll);
System.out.println(stack);
}
}