添加到ArrayList中从栈(JAVA) [英] Add to stack from ArrayList (Java)

查看:538
本文介绍了添加到ArrayList中从栈(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList pre-定义硬codeD值。我如何把它们添加到一个堆栈?这个想法是展示流行,推,偷看Stack类的功能。

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);

谢谢!

推荐答案

像许多集合类的堆栈提供了一个<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#addAll%28java.util.Collection%29\"相对=nofollow>的addAll 方式:

Like many collection classes, Stack provides a addAll method :

st.addAll(al)

这篇关于添加到ArrayList中从栈(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆