Scala吧~  - 讨论区

标题:Scala Tutorial 016:Stack容器简介与使用方法

2014年03月06日 星期四 09:53

Stack是一种“后进先出”(LIFO)的容器,主要的API是入栈(push)和出栈(pop)两种操作。

If you need a last-in-first-out sequence, you can use a Stack, which also comes in both mutable and immutable versions in the Scala collections library. You push an element onto a stack with push, pop an element with pop, and peek at the top of the stack without removing it with top.

编程示例如下:

import scala.collection.mutable.Stack
object S016_Stack {

	def main(args: Array[String]): Unit = {
		val s=Stack[Int]()
		for(i <- 0 until 10){
			s.push(i)
		}
		while(s.size > 0){
			println(s.pop)
		}
	}

}

参考资料:

http://www.artima.com/pins1ed/collections.html

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号