Scala 数组-Array

环境: CentOS 6.3

 

1. 定义且同时初始化数组

$ cat array.scala
val numNames2 = Array.apply("France","US","china")
println(numNames2(0))
[hadoop@localhost scala]$ scala array.scala
France

Array.apply("France","US","china") 等价于 Array("France","US","china")

2. 定义数组大小,然后初始化

$ cat array_init.scala
var array_init = new Array[String](3)
array_init(0) = "this "
array_init.update(1,"is ")
array_init.update(2,"china")
array_init.foreach(println)
$ scala array_init.scala
this 
is 
china

array_init.update(2,"china") 等价于 array_init(2) = "china"

你可能感兴趣的:(scala,array,数组)