【发布时间】:2019-07-28 11:15:42
【问题描述】:
我想找出接受数组(或列表)并附加到数据结构的最实用的方法。然后最后返回新的数据结构。
类似这样的:
def template(array: Array[String]): Array[Nothing] = {
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array){
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path
}
path_list(4)
}
但是,调用数据结构的单个索引作为检查存在性的一种简陋方式,path_list(4) 返回一个 Out of Bounds。
谢谢。
【问题讨论】:
-
我不确定
path_list(4)在那里做什么,但更惯用的解决方案是def template(values: List[String]) = values.map("s3//clone-staging" + _)。 -
只是试图从新的数据结构中返回一个索引来检查它是否存在。
标签: arrays scala list string-interpolation