数组 – 生成新数组,其中所有正数首先出现,然后是负数或零,但顺
发布时间:2020-12-16 18:21:04 所属栏目:安全 来源:网络整理
导读:这是 Scala for the Impatient的一个问题,Arrays的章节陈述为 Given an array of integers,produce a new array that contains all positive values of the original array,in their original order, followed by all values that are zero or negative,in t
这是
Scala for the Impatient的一个问题,Arrays的章节陈述为
我的尝试是 scala> val b = Array(-1,2,3,4,-10,-12) b: Array[Int] = Array(-1,-12) scala> val(positive,negative) = b partition(_ > 0) positive: Array[Int] = Array(2,4) negative: Array[Int] = Array(-1,-12) scala> positive ++ negative res11: Array[Int] = Array(2,-1,-12) 我可以在一条线上做得更好吗?我不确定 解决方法
考虑过滤器和过滤器,如下所示,
b.filter(_ > 0) ++ b.filterNot(_ > 0) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |