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 ?
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 ?
scala> val arr=Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
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)