Scala之ListBuffer、ArrayBuffer、Queue及Stack介紹
阿新 • • 發佈:2019-01-30
1、程式碼
2、結果package com.yy.base import scala.collection.immutable.Queue object BufferQueueAndStackTest extends App { //ListBuffer import scala.collection.mutable.ListBuffer val listBuffer = new ListBuffer[Int] listBuffer += 1 listBuffer += 2 println(listBuffer) //ArrayBuffer import scala.collection.mutable.ArrayBuffer val arrayBuffer = new ArrayBuffer[Int]() arrayBuffer += 1 arrayBuffer += 2 println(arrayBuffer) //不可變Queue val queue1 = Queue[Int]() //進隊:一個元素 val queue2 = queue1.enqueue(1) //進隊:一個List val queue3 = queue2.enqueue(List(2,3,4,5,6)) println(queue3) //拆分成兩個部分 val (element,rest) = queue3.dequeue println(element+":"+ rest) //可變Queue import scala.collection.mutable.Queue val queue_ = Queue[String]() queue_ += "a" queue_ ++= List("b","c") println(queue_) //返回第一個元素 println(queue_.dequeue()) //列印剩餘元素 println(queue_) //Stack:FIFO import scala.collection.mutable.Stack val stack = new Stack[Int] stack.push(1) stack.push(2) stack.push(3) println(stack.pop()) println(stack) println(stack.pop()) println(stack) }
ListBuffer(1, 2)
ArrayBuffer(1, 2)
Queue(1, 2, 3, 4, 5, 6)
1:Queue(2, 3, 4, 5, 6)
Queue(a, b, c)
a
Queue(b, c)
3
Stack(2, 1)
2
Stack(1)