call-by-value call-by-name
传值调用(call-by-value):先计算参数表达式的值,再应用到函数内部;
传名调用(call-by-name):将未计算的参数表达式直接应用到函数内部
辨析flatmap flatten map
(实际上并没有切)
var array = Array[Array[String]](Array("fsg"),Array("ssgs"))
//不支持flatmap
var st = Array[String]("sdg","fssgrhrhj")
st.flatMap(_.split(" ")).foreach(println)//sdg fssgrhrhj
var result: Array[String] = st.flatMap(_.split(" "))
var array = Array[Array[String]](Array("fsg"), Array("ssgs"))
val stringses1: Array[Array[String]] = array.flatMap(data => data.map(_.split("")))
val stringses: Array[String] = array.flatMap(data => data.flatMap(_.split("")))
//注意数组 和 集合
var st1 = Array[String]("ABCD", "abcd")
st1.flatMap(_.split("")).foreach(println)
st1.toList.flatMap(_.split("")).foreach(println)
val array1: Array[List[String]] = array.map(_.toList)
array1.toList.foreach(println)
(辨析扁平化操作)
object Test2 {
def main(args: Array[String]): Unit = {
val array = Array[String]("sdff fsewfgsgfsg ggg", "etwf wet tgwt tgwg")
//任然时候数组形式存在
val stringses: Array[Array[String]] = array.map(_.split(" "))
//扁平化操作
val flatten: Array[Char] = array.flatten
// println(flatten.toList)
array.map(_.split(" ")).flatten.toList.foreach(println)
//等价于flatmap算子
array.flatMap(_.split(" ")).toList.foreach(println)
//flatten只进行一次扁平操作
val stringses1: Array[Array[String]] = array.map(_.split(" "))
val flatten1: Array[String] = stringses1.flatten
val flatten2: Array[Char] = flatten1.flatten
//等价于flatmap算子
//把全部的array添加到array(array)中
array.flatMap(data => {
val strings: Array[String] = data.split(" ")
println(strings.toList)
println(strings.mkString(","))
strings
}).toList.foreach(println)
}
}
object Test1 {
def main(args: Array[String]): Unit = {
var list = List[String]("a,b,c,d","e,f,g")
list.flatMap(_.split(",")).foreach(println) //先进行map 进行对每一个部分"," 切分,之后进行扁平化(把List(Array)=>List(String))
list.map(_.split(",")).foreach(println) //List(Array)类型 地址值
val stringses = list.map(_.split(","))
for (index <- stringses){
println(index.mkString(","))
}
list.flatten.foreach(println)//包含","没有进行切分
}
}
网友评论