【发布时间】:2018-03-26 09:04:04
【问题描述】:
在 scala 中,您能否有一个 for 理解,它遍历对象列表,然后根据元素的属性之一的类型创建一个值数组?所以假设我有一个元素列表,每个元素都有一个属性,并且属性可以是不同的类型......
for (element <- elementList) element.attribute match {
case a: Type1 => "Type1"
case a => "All Types"
}
然后生成的 Array 将是一个具有类似值的数组
Array("Type1", "Type1", "All Types", "Type1", "All Types", "All Types", "All Types", "All Types")
【问题讨论】:
-
问题是什么?
-
我写的东西不起作用。问题是如何制作数组
标签: scala pattern-matching for-comprehension