import Array._ import scala.collection.mutable.Set object DataStructure { def main(args:Array[String]):Unit={ //數組 var z:Array[String] = new Array[String](3); var z1 = new Array[String](3); z(0)="hello"; z1(0)="world"; var z2 = Array("Runoob", "Baidu", "Google"); for(zz<-z2){ println(zz) } var myList = Array(1.9, 2.9, 3.4, 3.5) // 輸出全部數組元素 for ( x <- myList ) { println( x ) } // 計算數組全部元素的總和 var total = 0.0; for ( i <- 0 to (myList.length - 1)) { total += myList(i); } println("總和爲 " + total); // 查找數組中的最大元素 var max = myList(0); for ( i <- 1 to (myList.length - 1) ) { if (myList(i) > max) max = myList(i); } println("最大值爲 " + max); var myMatrix = ofDim[Int](3,3) // 建立矩陣 for (i <- 0 to 2) { for ( j <- 0 to 2) { myMatrix(i)(j) = j; } } // 打印二維陣列 for (i <- 0 to 2) { for ( j <- 0 to 2) { print(" " + myMatrix(i)(j)); } println(); } //合併數組 var myList1 = Array(1.9, 2.9, 3.4, 3.5) var myList2 = Array(8.9, 7.9, 0.4, 1.5) var myList3 = concat( myList1, myList2) // 輸出全部數組元素 for ( x <- myList3 ) { println( x ) } //建立區間數組 var myList4 = range(10, 20, 2) var myList5 = range(10,20) // 輸出全部數組元素 for ( x <- myList4 ) { print( " " + x ) } println() for ( x <- myList5 ) { print( " " + x ) } //List列表 // 字符串列表 val site: List[String] = List("Runoob", "Google", "Baidu") // 整型列表 val nums: List[Int] = List(1, 2, 3, 4) // 空列表 val empty: List[Nothing] = List() // 二維列表 val dim: List[List[Int]] = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) ) // 字符串列表 val site1 = "Runoob" :: ("Google" :: ("Baidu" :: Nil)) // 整型列表 val nums1 = 1 :: (2 :: (3 :: (4 :: Nil))) // 空列表 val empty1 = Nil // 二維列表 val dim1 = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Nil))) :: (0 :: (0 :: (1 :: Nil))) :: Nil /*** * head 返回列表第一個元素 tail 返回一個列表,包含除了第一元素以外的其餘元素 isEmpty 在列表爲空時返回true */ val site2 = "Runoob" :: ("Google" :: ("Baidu" :: Nil)) val nums2 = Nil println( "第一網站是 : " + site2.head ) println( "最後一個網站是 : " + site2.tail ) println( "查看列表 site 是否爲空 : " + site2.isEmpty ) println( "查看 nums 是否爲空 : " + nums2.isEmpty ) val site3 = "Runoob" :: ("Google" :: ("Baidu" :: Nil)) val site4 = "Facebook" :: ("Taobao" :: Nil) // 使用 ::: 運算符 var fruit = site3 ::: site4 println( "site3 ::: site4 : " + fruit ) // 使用 Set.:::() 方法 fruit = site3.:::(site4) println( "site3.:::(site4) : " + fruit ) // 使用 concat 方法 fruit = List.concat(site3, site4) println( "List.concat(site3, site4) : " + fruit ) val site5 = List.fill(3)("Runoob") // 重複 Runoob 3次 println( "site5 : " + site5 ) val num2 = List.fill(10)(2) // 重複元素 2, 10 次 println( "num2 : " + num2 ) // 經過給定的函數建立 5 個元素 val squares = List.tabulate(6)(n => n * n) println( "一維 : " + squares ) // 建立二維列表 val mul = List.tabulate( 4,5 )( _ * _ ) println( "多維 : " + mul ) //反轉 val site6 = "Runoob" :: ("Google" :: ("Baidu" :: Nil)) println( "site6 反轉前 : " + site6 ) println( "site6 反轉後 : " + site6.reverse ) //Set集合 val site7 = Set("Runoob", "Google", "Baidu") val nums6: Set[Int] = Set() println( "第一網站是 : " + site7.head ) println( "最後一個網站是 : " + site7.tail ) println( "查看列表 site 是否爲空 : " + site7.isEmpty ) println( "查看 nums 是否爲空 : " + nums6.isEmpty ) // ++ 做爲運算符使用--鏈接 var site8 = site7 ++ nums6 println( "site1 ++ site2 : " + site8 ) val site10 = Set("Faceboook", "Taobao") // ++ 做爲方法使用 site8 = site7.++(site10) println( "site1.++(site2) : " + site8 ) val num = Set(5,6,9,20,30,45) // 查找集合中最大與最小元素 println( "Set(5,6,9,20,30,45) 集合中的最小元素是 : " + num.min ) println( "Set(5,6,9,20,30,45) 集合中的最大元素是 : " + num.max ) val num1 = Set(5,6,9,20,30,45) val num3 = Set(50,60,9,20,35,55) // 交集 println( "num1.&(num2) : " + num1.&(num3) ) println( "num1.intersect(num2) : " + num1.intersect(num3) ) //Map映射 var A:Map[Char,Int] = Map() A += ('I' -> 1) A += ('J' -> 5) A += ('K' -> 10) A += ('L' -> 100) println( "colors 中的鍵爲 : " + A.keys ) println( "colors 中的值爲 : " + A.values ) println( "檢測 colors 是否爲空 : " + A.isEmpty ) //鏈接 val colors1 = Map("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F") val colors2 = Map("blue" -> "#0033FF", "yellow" -> "#FFFF00", "red" -> "#FF0000") // ++ 做爲運算符 var colors = colors1 ++ colors2 println( "colors1 ++ colors2 : " + colors ) // ++ 做爲方法 colors = colors1.++(colors2) println( "colors1.++(colors2)) : " + colors ) //foreach循環 val sites = Map("runoob" -> "http://www.runoob.com", "baidu" -> "http://www.baidu.com", "taobao" -> "http://www.taobao.com") sites.keys.foreach{ i => print( "Key = " + i ) println(" Value = " + sites(i) )} if( sites.contains( "runoob" )){ println("runoob 鍵存在,對應的值爲 :" + sites("runoob")) }else{ println("runoob 鍵不存在") } if( sites.contains( "baidu" )){ println("baidu 鍵存在,對應的值爲 :" + sites("baidu")) }else{ println("baidu 鍵不存在") } if( sites.contains( "google" )){ println("google 鍵存在,對應的值爲 :" + sites("google")) }else{ println("google 鍵不存在") } //Iterator(迭代器) /** * Scala Iterator(迭代器)不是一個集合,它是一種用於訪問集合的方法。 迭代器 it 的兩個基本操做是 next 和 hasNext。 調用 it.next() 會返回迭代器的下一個元素,而且更新迭代器的狀態。 調用 it.hasNext() 用於檢測集合中是否還有元素。 讓迭代器 it 逐個返回全部元素最簡單的方法是使用 while 循環: */ val it = Iterator("Baidu", "Google", "Runoob", "Taobao") while (it.hasNext){ println(it.next()) } val ita = Iterator(20,40,2,50,69, 90) val itb = Iterator(20,40,2,50,69, 90) println("最大元素是:" + ita.max ) println("最小元素是:" + itb.min ) println("ita.size 的值: " + ita.size ) println("itb.length 的值: " + itb.length ) } }