3,Tuple、Array、Map与文件操作入门实战


package com.dt.scala.hello

object ArrayOperations {

  def main(args: Array[String]) {
    val array = Array(1, 2, 3, 4, 5)

    for (i <- 0 until array.length) {
      println(array(i))
    }

    for (elem <- array) println(elem)
  }
}

package com.dt.scala.hello
import scala.io.Source

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

package com.dt.scala.hello

object MapOperations {
  def main(args: Array[String]) {
    val ages = Map("Rocky" -> 27, "Spark" -> 5)

    for ((k, v) <- ages) {
      println("Key is " + k + ",value is " + v)
    }

    for ((k, _) <- ages) { //placeholder
      println("Key is " + k)
    }
  }
}

package com.dt.scala.hello

object TupleOps {

  def main(args: Array[String]): Unit = {
    val triple = (100, "Scala", "Spark")
    println(triple._1)
    println(triple._2)
    println(triple._3)
  }
}


信息来源于 DT大数据梦工厂微信公众账号:DT_Spark

网址:http://pan.baidu.com/share/home?uk=4013289088&view=share#category/type=0





你可能感兴趣的:(3,Tuple、Array、Map与文件操作入门实战)