Sunday, December 16, 2012

Convert from List[Any] to Array[String] in Scala





Convert List[Any] to Array[String]

scala> val stringAry: Array[String]  = (List("a", 1) map (_.toString)).toArray
stringAry: Array[String] = Array(a, 1)



Create typeOf method to determine the type of each element in array

scala> def typeOf[T: Manifest](t: T): Manifest[T] = manifest[T]
typeOf: [T](t: T)(implicit evidence$1: Manifest[T])Manifest[T]



Print results

scala> stringAry.foreach( i => println(typeOf(i)) )
java.lang.String
java.lang.String



References





Sponsor Ads


2 comments: