views:

147

answers:

2

I'm new to Scala ,just started learning it today.I would like to know how to initialize an array in scala.

Example Java code

String[] arr={"Hello","World"};

What is the equivalent of the above code in Scala ?

+16  A: 
scala> val arr=Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
Vasil Remeniuk
+4  A: 

Additional to Vasil's answer: If you have the values given as a Scala collection, you can write

val list = List(1,2,3,4,5)
val arr = Array[Int](list:_*)
println(arr.mkString)

But usually the toArray method is more handy:

val list = List(1,2,3,4,5)
val arr = list.toArray
println(arr.mkString)
Landei