本文介绍了从 ArrayList (Java) 添加到堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用硬编码值预定义的 ArrayList.如何将这些添加到堆栈中?思路是演示栈类的pop、push、peek功能.
I have an ArrayList pre-defined with hardcoded values. How do I add these to a stack? The idea is to demonstrate the pop, push, peek functions of the stack class.
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);
谢谢!
推荐答案
像许多集合类一样,Stack 提供了一个 addAll 方法:
Like many collection classes, Stack provides a addAll method :
st.addAll(al)
这篇关于从 ArrayList (Java) 添加到堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!