scala入门系列(六) - 集合 Array Map List

array

object ArrayApp {
  def main(args: Array[String]): Unit = {
    //定长
    val str = new Array[String](5)
    println(str.length)//5
    //println(str.toString)

    
    //mkstring
    val b = Array("aaa","bbb","ccc")

    println(b.mkString(""))//aaabbbccc
    println(b.mkString("$"))//aaa$bbb$ccc 按$分割
    println(b.mkString("<","$",">")) //加上前后标记 

    val c = new StringBuilder
    val d = "rr"
    c append d //==c.append(d)
    c.append(d)
    println(c)
    //变长 
    val e = ArrayBuffer[String]()
    println(e.length)
    e.append("ww")
    println(e.length)
    e.insert(1,"22","22","tt")
    e.remove(1)
    println(e)
  }


}

map

object MapApp {
  def main(args: Array[String]): Unit = {
 
   //定长
    val map =  Map("a"->2,"b"->3)
    println(map.get("a").getOrElse()) 
    //println(map.get("c").get)// None.get
    println(map.get("a").getOrElse())
   val a = mutable.Map[String,Int]()

   // mutable.Map 变长
    val b = mutable.HashMap[String,Int]()
    b("aaa")=2

    println(b("aaa"))
    b +=("bbb"->3)
    //map循环
    for((key,value)<-b){
      println(key+":"+value)
    }
    for (key<-b.keySet){
      println(key+":"+ b.get(key))
    }
    for(value<-b.values){
      println(value)
    }
  }
}

List

object ListApp {
  def main(args: Array[String]): Unit = {
    println(Nil)//Nil是一个空的 List()
    //定长
    val a = List(1,2,3,4)
   // val b = new  List
    //head 头
    println(a.head)//1
    //tail 尾
    println(a.tail)//List(2, 3, 4)
   // a :: 5 ::Nil  拼接
    //a+=6
    println(a)
    //变长
    val l = ListBuffer[Int]()
    l+=2
    println(l)
  }
}

你可能感兴趣的:(大数据,scala)