首页 > 代码库 > 一天一段scala代码(三)

一天一段scala代码(三)

一天一段scala代码(三)

       为了更好的驾驭spark,最近在学习scala语言特性,主要看《快学scala》,顺便把一些自己认为有用的代码记下来。

package examples

import scala.collection.mutable.ArrayBuffer

object Example3 {
  def main(args: Array[String]) 
  {
    
     val start = System.currentTimeMillis
     println("took " + (System.currentTimeMillis - start) + " ms")
     
     //定长数组Array
     val nums =  new Array[Int](3)   
     nums.foreach(println)
 
     val numArray =   Array[Int](3,2)   
     numArray.foreach(println)
     
     //变长数组ArrayBuffer
     val charArray = ArrayBuffer[Char]()
     charArray += 'H'
     charArray += 'i'
     charArray += (',','g')
     charArray ++= Array('i','r','l')
     charArray.foreach { println }
     charArray.trimEnd(5)
     //还有各种insert,remove等
     
     
     val a = 0 until (10,3)
     println(a)
     
     val intArray = a.toArray
     val intArray2 = for(ele<-intArray if ele%2==0) yield 2*ele
     intArray2.foreach(println)
     
     val intArray3 = intArray.filter { _%2==0}.map { 2*_ }
     intArray3.foreach { println }
     
     
    println( intArray3.mkString("[", ",", "]"))
     
     
     val matrix = Array.ofDim[Double](3,4) //3行4列的数组
     
     
     
  }
}

输出
took 54 ms
0
0
0
3
2
H
i
,
g
i
r
l
Range(0, 3, 6, 9)
0
12
0
12
[0,12]

本文链接:http://blog.csdn.net/lingerlanlan/article/details/43280789
本文作者:linger

一天一段scala代码(三)