2017-02-16 6 views
2

私は結合してArray[Item]を作成したい2つの配列を持っています。 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

あなたItem.tupledとアレイの上mapことができます:

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)) 
関連する問題