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);
Thanks!
Like many collection classes, Stack provides a addAll method :
st.addAll(al)
Why not just iterate over array list and push it to stack?
for(String str : al)
st.push(str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With