点击(此处)折叠或打开
- def main(args: Array[String]) {
- import scala.collection.mutable.ListBuffer
- val listBuffer = new ListBuffer[Int]
- listBuffer += 1
- listBuffer += 2
- println(listBuffer) //结果:ListBuffer(1, 2)
-
- import scala.collection.mutable.ArrayBuffer
- val arrayBuffer = new ArrayBuffer[Int]()
- arrayBuffer += 1
- arrayBuffer += 2
- println(arrayBuffer) //结果:ArrayBuffer(1, 2)
-
- val empty = Queue[Int]() //不可变Queue(immutable)
- val queue1 = empty.enqueue(1) //追加元素之后,产生一个新的Queue
- val queue2 = queue1.enqueue(List(2,3,4,5))
- println(queue2) //结果:Queue(1, 2, 3, 4, 5)
- val (element, left) = queue2.dequeue //将Queue拆分成两部分,第一个元素和剩下的元素
- println(element + " : " + left) //结果:1 : Queue(2, 3, 4, 5)
-
- import scala.collection.mutable.Queue
- val queue = Queue[String]()
- queue += "a"
- queue ++= List("b", "c")
- println(queue) //结果:Queue(a, b, c)
- println(queue.dequeue) //和不可变Queue的dequeue方法不同,这里返回Queue的第一个元素,并且移除第一个元素
- println(queue) //结果:Queue(b, c)
-
- import scala.collection.mutable.Stack
- val stack = new Stack[Int]
- stack.push(1)
- stack.push(2)
- stack.push(3)
- println(stack.top) //返回栈顶元素,3,但是不会弹出
- println(stack) //结果依然是:Stack(3, 2, 1)
- println(stack.pop) //弹出栈顶元素
- println(stack) //结果:Stack(2, 1)
-
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28912557/viewspace-1966105/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28912557/viewspace-1966105/