简体   繁体   English

Scala类型不匹配的基础类型

[英]Scala Type Mismatch underlying type

I'm writing methods to convert Set[Tuple2[String, String]] to String and vice versa. 我正在编写将Set[Tuple2[String, String]]String ,反之亦然。
I'm saving the string value as v1,v2#v3,v4#v5,v6 我将字符串值保存为v1,v2#v3,v4#v5,v6
In order to fill the Set I'm splitting the string by ',' and in order to extract the values I'm trying to split each value by '#' but i receive 为了填充Set我将字符串拆分为','并且为了提取值,我试图将每个值除以'#'但我收到

type mismatch: found: x.type (with underlying type Array[String] type mismatch:found:x.type(带底层类型Array [String]

The code I tried using is 我尝试使用的代码是

val x = overwriters.split("#")
for(tuple <- x) {
  tuple.split(",")
}

The returned type of split is an array of String so it is not clear to me why i cannot split each member of the returned array 返回的split类型是一个String数组,因此我不清楚为什么我不能拆分返回数组的每个成员

overwrites.split("#").map(_.split(",")).map(x=> (x(0),x(1))).toSet

这将以更加惯用的方式实现同​​样的目标。

tuple.split(",") returns an array of two elements. tuple.split(",")返回两个元素的数组。 You need to convert it to a tuple. 您需要将其转换为元组。

val overwriters ="v1,v2#v3,v4#v5,v6"              
val x = overwriters.split("#").toSet
for(tuple <- x) yield {
  val t = tuple.split(",")
  (t(0),t(1))
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM