scala学习之路:5.懒加载以及数组的操作

import scala.io.Source

object dt06_Lazy {
  def main(args: Array[String]): Unit = {
    
    // 懒加载用的时候才创建
    lazy val file = Source.fromFile("/Users/ycf/Desktop/traslate_h.txt")
    println("Scala")
    
//    知道这里使用才会 调用 Source.fromFile的功能检验 路径是否正确   
    for(line <- file.getLines()) println(line)
    
  }


新建worksheet

scala学习之路:5.懒加载以及数组的操作_第1张图片




import scala.io.Source

object ScalaAction {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
     
    // 懒加载
    lazy val file = Source.fromFile("/Users/ycf/Desktop/traslate.txt")
                                                  //> file: => scala.io.BufferedSource
    println("Scala")                              //> Scala
    
    file                                          //> res0: scala.io.BufferedSource = non-empty iterator
// 	到用的时候 才拿出来  
    for(line <- file.getLines()) println(line)    //> "welcome"="点击屏幕继续...";
                                                  //| 
                                                  //| //首页
                                                  //| "tabBarHome"="首页";
                                                  //| "citySele
}


数组的操作

import scala.collection.mutable.ArrayBuffer

object dt07ArrayOps {
  def main(args: Array[String]): Unit = {
    val nums = new Array[Int](10) //> nums  : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    val a = new Array[String](10) //> a  : Array[String] = Array(null, null, null, null, null, null, null, null, n
    //| ull, null)

    val s = Array("Hello", "world") //> s  : Array[String] = Array(Hello, world)
    s(0) = "Goodby"
    s //> res0: Array[String] = Array(Goodby, world)

    nums(0) = 10

    val b = ArrayBuffer[Int](10) //> b  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10)
    b += 1 //> res1: com.scala02.tuple.ScalaInAction.b.type = ArrayBuffer(10, 1)
    b += (2, 3, 5) //> res2: com.scala02.tuple.ScalaInAction.b.type = ArrayBuffer(10, 1, 2, 3, 5)
    b ++= Array(22, 11, 33) //> res3: com.scala02.tuple.ScalaInAction.b.type = ArrayBuffer(10, 1, 2, 3, 5, 2
    //| 2, 11, 33)

    b.trimEnd(5)
    b.insert(2, 6)
    b.insert(2, 7, 8, 9)

    b.remove(2) //> res4: Int = 7

    b //> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 1, 8, 9, 6
    //| , 2)

    val c = b.toArray //> c  : Array[Int] = Array(10, 1, 8, 9, 6, 2)

    c //> res6: Array[Int] = Array(10, 1, 8, 9, 6, 2)

    for (i <- 0 until a.length)
      println(i + ":" + a(i)) //> 0:null
    //| 1:null
    //| 2:null
    //| 3:null
    //| 4:null
    //| 5:null
    //| 6:null
    //| 7:null
    //| 8:null
    //| 9:null

    val c2 = Array(5, 6, 10, 2, 3) //> c2  : Array[Int] = Array(5, 6, 10, 2, 3)
    val result = for (elem <- c2) yield 2 * elem //> result  : Array[Int] = Array(10, 12, 20, 4, 6)

    for (elem <- c if elem % 2 == 0) yield 2 * elem //> res7: Array[Int] = Array(20, 16, 12, 4)
    // 过滤后的偶数 结果 x3
    c2.filter(_ % 2 == 0).map(3 * _) //> res8: Array[Int] = Array(18, 30, 6)
    c2 //> res9: Array[Int] = Array(5, 6, 10, 2, 3)

    val d = ArrayBuffer(1, 7, 2, 3) //> d  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 3)
    val bSorted = d.sorted //> bSorted  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 
    //| 7)
    val e = Array(1, 7, 2, 9) //> e  : Array[Int] = Array(1, 7, 2, 9)
    scala.util.Sorting.quickSort(e)

    e //> res10: Array[Int] = Array(1, 2, 7, 9)
    e.mkString //> res11: String = 1279
    e.mkString("<", ",", ">") //> res12: String = <1,2,7,9>

    val matrix = Array.ofDim[Double](3, 4) //> matrix  : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0,
    //|  0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))
    matrix(2)(1) = 42
    matrix //> res13: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.
    //| 0, 0.0, 0.0), Array(0.0, 42.0, 0.0, 0.0))
    val triangle = new Array[Array[Int]](10) //> triangle  : Array[Array[Int]] = Array(null, null, null, null, null, null, n
    //| ull, null, null, null)
    for (i <- 0 until triangle.length)
      triangle(i) = new Array[Int](i + 1)

    triangle //> res14: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Arr
    //| ay(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 
    //| 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0
    //| , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

  }

}


你可能感兴趣的:(scala学习之路:5.懒加载以及数组的操作)