从ArrayList(Java)添加到堆栈

A C*_*A C 8 java stack arraylist

我有一个预定义了硬编码值的ArrayList.如何将这些添加到堆栈?这个想法是为了演示堆栈类的pop,push,peek函数.

ArrayList<String> al = new ArrayList<String>();

al.add("A");
al.add("B");
al.add("C");

Stack<String> st = new Stack<String>();

st.push(al); **// This doesn't seem to work.. Will I have to loop it in some way?**

System.out.println(st);
Run Code Online (Sandbox Code Playgroud)

谢谢!

Den*_*ret 25

像许多集合类一样,Stack提供了一个addAll方法:

st.addAll(al)
Run Code Online (Sandbox Code Playgroud)