考虑以下Scala数组x
定义
scala> val x = Array(10, 32, 45, 54, 44, 37)
x: Array[Int] = Array(10, 32, 45, 54, 44, 37)
提供和索引selector
,并保证不包含越界索引值:
scala> val selector = Seq(0, 3, 5)
selector: Seq[Int] = List(0, 3, 5)
在Scala中,将selector
与x
结合使用的简洁方法是,只选择x
中与selector
中所需索引匹配的result
索引一个新的result
数组?具体而言,result: Array(10, 54, 37)
预计为:
--------- beginning of crash
10-24 21:32:35.860 5988 5988 E AndroidRuntime: FATAL EXCEPTION: main
10-24 21:32:35.860 5988 5988 E AndroidRuntime: Process: com.android.dialer, PID: 5988
10-24 21:32:35.860 5988 5988 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.InstantiationException: java.lang.Class<android.support.v4.content.FileProvider> has no zero argument constructor
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6290)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.installContentProviders(ActivityThread.java:5851)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5772)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.access$2000(ActivityThread.java:173)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:105)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6541)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: Caused by: java.lang.InstantiationException: java.lang.Class<android.support.v4.content.FileProvider> has no zero argument constructor
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at java.lang.Class.newInstance(Native Method)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6274)
10-24 21:32:35.860 5988 5988 E AndroidRuntime: ... 10 more
10-24 21:32:35.863 841 852 W ActivityManager: Force finishing activity com.android.dialer/.app.DialtactsActivity
10-24 21:32:35.869 841 857 I ActivityManager: Showing crash dialog for package com.android.dialer u0
感谢任何帮助。谢谢。
答案 0 :(得分:5)
事件缩短:)
import scala.collection.breakOut
val result: Array[Int]= selector.map(x)(breakOut)
.map
采用Function1[Int,_]
类型的参数,当我们在那里传递x
时,编译器足够聪明地弄明白,我的意思是x.apply
,这是一个方法Int => Int
,它使用 eta-expansion 将其转换为函数,这样我们就不必明确拼写出来,创建一个像匿名函数一样的函数.map { idx => x(idx) }
。
breakOut
是一个集合工厂,它告诉.map
将结果返回为Array
而不是List
(因为LHS上的预期返回类型为{{1} }})。这样可以避免构建中间集合,然后将其复制到(Array
)
答案 1 :(得分:1)
使用地图访问x
每个selector
。
scala> selectors.map(s => x(s)).toArray
res1: Array[Int] = Array(10, 54, 37)