2017-02-16 42 views
2

我有两个阵列,我喜欢合并并创建一个Array[Item]Item是一个案例类。这里有一个例子:从Tuple2数组创建案例类对象的最简单方法是什么?

case class Item(a: String, b: Int) 

val itemStrings = Array("a_string", "another_string", "yet_another_string") 

val itemInts = Array(1, 2, 3) 

val zipped = itemStrings zip itemInts 

目前我采用如下方案来解决,但我不知道是否有其他的可能性这个...

val itemArray = zipped map { case (a, b) => Item(a, b) } 

,带出我想要的东西:

itemArray: Array[Item] = Array(Item(a_string, 1), Item(another_string, 2), Item(yet_another_string, 3)) 

我也试过这个,但它不适用于一系列元素:

(Item.apply _).tupled(zipped:_*) 

Item.tupled(zipped:_*) 

回答

3

您可以map在与Item.tupled数组:

zipped.map(Item.tupled) 

scala> zipped.map(Item.tupled) 
res3: Array[Item] = Array(Item(a_string,1), Item(another_string,2), Item(yet_another_string,3)) 
相关问题