day02-scala

来源:互联网 发布:天刀太白少女捏脸数据 编辑:程序博客网 时间:2024/05/02 01:33

1 tuple

object TupleOps {  def main(args: Array[String]): Unit = {  val triple=(100,"spark","hello")<pre name="code" class="java">//数组object ArrayOperrations {  def main(args: Array[String]): Unit = {    val array= Array(1,2,3,4)    for(elem<-array)      println(elem)  }}

println(triple._1) println(triple._2) println(triple._3)}}

结果:

100sparkhello

2 Array

//数组object ArrayOperrations {  def main(args: Array[String]): Unit = {    val array= Array(1,2,3,4)    for(elem<-array)      println(elem)  }}


结果:

1

2

3

4


3 map

object MapOperation {  def main(args: Array[String]): Unit = {    val map=Map("Rocky"->25,"james"->89)    for((k,_)<-map){//只想打印key时候,将value写成_      println("Key:"+k)    }  }}

结果:

Key:Rocky
Key:james


object MapOperation {  def main(args: Array[String]): Unit = {    val map=Map("Rocky"->25,"james"->89)        for((k,v)<-map){      println("KEY:"+k+"value:"+v)    }  }}
结果:

KEY:Rockyvalue:25
KEY:jamesvalue:89



4 file

import scala.io.Sourceobject FileOps {  def main(args: Array[String]) {    val file=Source.fromFile("E:\\spark.txt")//指定本地的文件名    for(line<-file.getLines){      /*       * 之前我写了两行出错了,在spark.txt中要写一行,而不是两行,请逐行添加       */      println(line)    }  }}
import scala.io.Sourceobject FileOps {  def main(args: Array[String]) {   val file=Source.fromURL("http://www.hao123.com")//指定一个网址    for(line<-file.getLines){      /*       * 之前我写了两行出错了,在spark.txt中要写一行,而不是两行,请逐行添加       */      println(line)    }  }}









0 0