雲星數據---Scala實戰系列(精品版)】:Scala入門教程043-Scala實戰源碼-Scala Set操作

Scala Set操作

package scala_learn.demo11_Collection

/**
 * Created by liguohua on 2017/3/1.
 */

object O3_Set {
  def main(args: Array[String]) {
    test3()
  }

  def test3(): Unit = {
    val st = Set(3, 5, 7, 89)
    st match {
      case s: Set[Int] => println("right Set")
      case _ => println("error")
    }
  }

  def test2(): Unit = {
    //創建一個Set集合對象,其原型可以理解爲Set(e:Any*)
    val st = Set(3, 5, 7, 89)
    //遍歷Set集合進行取值
    val it = st.iterator
    while (it.hasNext) {
      print(it.next() + "\t")
    }
  }

  def test1(): Unit = {
    //創建一個Set集合對象
    val st = Set(3)
    //Set集合對象的大小爲1
    println(st.size)
    //Set集合的第一個值爲3
    println(st.head)
    //Set集合不能用下標進行取值
    //println(st(0))//false
    // Set集合中的元素不可以改變,scala.collection.immutable.Set
    //st.head=8//錯誤,Set集合中的元素不可以改變
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章