Scala 深刻淺出實戰經典 第39講:ListBuffer、ArrayBuffer、Queue、Stack操做代碼實戰

王家林親授《DT大數據夢工廠》大數據實戰視頻 Scala 深刻淺出實戰經典(1-64講)完整視頻、PPT、代碼下載:
百度雲盤:http://pan.baidu.com/s/1c0noOt6
騰訊微雲:http://url.cn/TnGbdC
360雲盤:http://yunpan.cn/cQ4c2UALDjSKy 訪問密碼 45e2
技術愛好者尤爲是大數據愛好者 能夠加DT大數據夢工廠的qq羣socket

DT大數據夢工廠① :462923555
DT大數據夢工廠②:437123764
DT大數據夢工廠③ :418110145ide

本視頻由王家林老師, 親自講解, 徹底經過代碼實戰把您帶人大數據的時代.大數據

 

 1 package com.parllay.scala.dataset
 2 
 3 /**
 4  * Created by richard on 15-7-23.
 5  */
 6 object ListBuffer_ListArray_Queue_Stack {
 7 
 8   def main(args: Array[String]) {
 9 
10     /**Scala 深刻淺出實戰經典 第39講:ListBuffer、ArrayBuffer、Queue、Stack操做代碼實戰
11      * mutable集合是可變的, 在這裏咱們建立的是可變的listBuffer
12      * 並動態的給list賦值.
13      */
14     import scala.collection.mutable.ListBuffer
15     val listBuffer = new ListBuffer[Int]
16     listBuffer += 1
17     listBuffer += 10
18     println(listBuffer)//ListBuffer(1, 10)
19 
20     /**
21      * 在這裏咱們建立的是可變的ArrayBuffer
22      * 並動態的給arrayBuffer賦值.
23      */
24     import scala.collection.mutable.ArrayBuffer
25     val arrayBuffer = new ArrayBuffer[Int]()
26     arrayBuffer += 1
27     arrayBuffer += 1222222
28     println(arrayBuffer)//ArrayBuffer(1, 1222222)
29 
30     /**
31      * 在這裏Queue是不可變的, queue1, queue2其實是生成了新的對象,
32      * 追加值須要使用enqueue方法
33      *
34      */
35     import scala.collection.immutable.Queue
36     val empty = Queue[Int]()
37     val queue1 = empty.enqueue(1)
38     val queue2 = queue1.enqueue(List(2,3,4,5))
39     println(queue2)
40 
41     /**
42      * dequeue方法的執行結果是把原來的queue分拆成兩部分,
43      * 一部分是會第一個元素,另一部分是剩下的元素組成的新的queue.
44      */
45     val (element, left) = queue2.dequeue
46     println(element + ":" + left)
47 
48 
49     /**
50      * 隊列是先進先出
51      * 下面這個是可變的Queue, 和上面的不可變的Queue進行比較發現
52      * 在這裏咱們用一樣的常量queue就能夠動態的增長隊列的大小.
53      */
54     val queue = scala.collection.mutable.Queue[String]()
55     queue += "a"
56     queue ++= List("hello", "Spark")
57     println(queue)
58     println(queue.dequeue)//把隊列的第一個元素刪除
59     println(queue)//這時候的隊列長度會變小
60 
61     /**
62      * 可變的stack, 先進後出
63      */
64     import scala.collection.mutable.Stack
65     val stack = new Stack[Int]
66     stack.push(1)
67     stack.push(2)
68     stack.push(3)
69     println(stack.pop)
70     println(stack)
71     println(stack.pop)
72     println(stack)
73 
74     /**
75      * ListBuffer(1, 10)
76       ArrayBuffer(1, 1222222)
77       Queue(1, 2, 3, 4, 5)
78       1:Queue(2, 3, 4, 5)
79       Disconnected from the target VM, address: '127.0.0.1:46928', transport: 'socket'
80       Queue(a, hello, Spark)
81       Queue(hello, Spark)
82       Stack(2, 1)
83       Stack(1)
84      */
85 
86 
87 
88 
89   }
90 
91 }
View Code
相關文章
相關標籤/搜索