SCALA入门(3)

这一节学习TUPLE,ARRAY,MAP

Tuple
package com.scala.study
object HelloScala {
    def main(args:Array[String]){
        var pair = (100,"Scala","Spark")
        println(pair._1)
        println(pair._2)
    }
}
Array
package com.scala.study
object HelloScala {
    def main(args:Array[String]){
       var array = Array(1,2,3,4,5)
       for(i<-0 until array.length){
         println(array(i))
       }
       
       for(elem<-array)
         println(elem)
    }
}
Map
package com.scala.study
object HelloScala {
    def main(args:Array[String]){
     var ages = Map("Rocky"->27,"Spark"->5)
     for((k,v)<-ages){
       println("Key is" + k +",value is "+ v)
     }
      for((k,_)<-ages){
       println("Key is" + k )
     }
}
}

文件操作

package com.scala.study

import scala.io.Source

object HelloScala {
  def main(args: Array[String]) {
    var file = Source.fromFile("E:\\temp.txt");
    for (line <- file.getLines())
      println(line)
      
    var file1 = Source.fromURL("http://spark.apache.org/")
    for (line <- file1.getLines())
      println(line)
  }
}

相关推荐