scala学习之路:元组数组集合

object TupleOps {
  
  def main(args: Array[String]): Unit = {
//    元组 可以包含不同的类型 
    val triple = (100 ,"Acala","Java")
    
    println(triple._1)
    println(triple._2)
    println(triple._3 +" " +triple._3.getClass )
//100
//Acala
//Java class java.lang.String


  }
  
}


object ArrayOperation {
  def main(args: Array[String]): Unit = {
    
    val array = Array(1,2,3,4,5)
    
    for(i <- 0 until array.length) println(array(i))
    println("-=-=-=-=-=-=-=-=-=-=-=--=-=-=")
    for(elem <- array) println(elem)
    
  }
}

打印结果:

1

2

3

4

5

-=-=-=-=-=-=-=-=-=-=-=--=-=-=

1

2

3

4

5


object dt03MapOperation {
  def main(args: Array[String]): Unit = {

    val ages = Map("Rocky" -> 27, "spark" -> 5)
          for((k,v) <- ages) {
            
            println("key is "+ k+", value is "+v)
            
          }
    println("-=-=-=-=-=-=-=-=-=-=-=--=-=-=")
    for ((k, _) <- ages) { // “_”是占位符

      println("key is " + k)

    }

  }

}
/** 打印结果 :
key is Rocky, value is 27
key is spark, value is 5
-=-=-=-=-=-=-=-=-=-=-=--=-=-=
key is Rocky
key is spark

 */
import scala.io.Source

object dt04 {
  def main(args: Array[String]): Unit = {

    //  1.  读取本地文件 打印出文件的内容
    //    val file = Source.fromFile("/Users/ycf/Desktop/traslate_m.txt")
    //  2.  读取URL地址 会打印出网页的源码
    val file = Source.fromURL("http://spark.apache.org")

    for (line <- file.getLines()) println(line)

  }
}






你可能感兴趣的:(scala学习之路:元组数组集合)