ListBuffer、ArrayBuffer、Queue、Stack操作代码实战

ListBuffer、ArrayBuffer、Queue、Stack操作代码实战 
  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]()
    val queue1 = empty.enqueue(1)
    val queue2 = queue1.enqueue(List(2,3,4,5))
    println(queue2)
    val (element, left) = queue2.dequeue
    println(element + " : " + left)
  运行情况:
Queue(1, 2, 3, 4, 5)
1 : Queue(2, 3, 4, 5)

 


    import scala.collection.mutable.Queue
    val queue = Queue[String]()
    queue += "a"
    queue ++= List("b", "c")
    println(queue)
    println(queue.dequeue)
    println(queue)
 运行情况:
Queue(a, b, c)
a
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)
    println(stack)
    println(stack.pop)
    println(stack)
运行情况:
3
Stack(3, 2, 1)
3
Stack(2, 1)
     

 

 

DT大数据微信公众账号:DT_Spark
DT大数据梦工厂scala的所有视频、PPT和代码在百度云盘的链接地址:http://pan.baidu.com/share/home?

uk=4013289088&view=share#category/type=0&qq-pf-to=pcqq.group

 

你可能感兴趣的:(ListBuffer、ArrayBuffer、Queue、Stack操作代码实战)