Scala字符串类型歧义
我想删除scala Seq中的一些元素,但会出现令人困惑的错误:Scala字符串类型歧义,scala,generics,Scala,Generics,我想删除scala Seq中的一些元素,但会出现令人困惑的错误: <console>:33: error: type mismatch; found : Seq[java.lang.String] required: Seq[String(in method drop_Val_List_In_List)] tmp; ^ 所需的输出:包含“car”和“road”字符串的tmp我更改了一些类型,现在它可以工作了 def drop_Va
<console>:33: error: type mismatch;
found : Seq[java.lang.String]
required: Seq[String(in method drop_Val_List_In_List)]
tmp;
^
所需的输出:包含“car”和“road”字符串的tmp我更改了一些类型,现在它可以工作了
def drop_Val_In_List[String](ls: Seq[String], value: String): Seq[String] = {
val index = ls.indexOf(value) //index is -1 if there is no matc
if (index < 0) {
ls
} else if (index == 0) {
ls.tail
} else {
// splitAt keeps the matching element in the second group
val (a, b) = ls.splitAt(index)
a ++ b.tail
}
}
def drop_Val_List_In_List[String](ls: Seq[String], in_ls: Seq[String]): Seq[String] = {
var tmp_ = ls
//println(tmp.getClass)
for(x <- in_ls ){ // should work without var x
println(x)
tmp_ = drop_Val_In_List(tmp_, x)
}
tmp_
}
val KeepCols = Seq("ctn", "type", "month", "car", "road")
val tmp = drop_Val_List_In_List(KeepCols, List("ctn", "type", "month"))// Start writing your ScalaFiddle code here
println(tmp)
// Start writing your ScalaFiddle code here
def drop_Val_In_List[String](ls:Seq[String],value:String):Seq[String]={
val index=ls.indexOf(value)//如果没有matc,则索引为-1
如果(指数<0){
ls
}else if(索引==0){
鱼尾
}否则{
//splitAt将匹配元素保留在第二个组中
val(a,b)=ls.splitAt(索引)
a++b.tail
}
}
def drop_Val_List_In_List[String](ls:Seq[String],In_ls:Seq[String]):Seq[String]={
var tmp_uLS=
//println(tmp.getClass)
for(xdef drop\u Val\u List\u In_List[String]
声明一个类型参数,您可以调用该类型参数String
,该类型参数将“real”String
隐藏起来
如果您想对任何类型的A
执行此操作,请将那里的字符串更改为A
。如果您只想专门处理String
,请删除[String]
,并将def drop\u Val\u List\u保留在\u列表中(…)
旁注:def drop\u Val\u List\u在\u列表中[A](ls:Seq[A],在\u ls:Seq[A])=ls.foldLeft(在\ls中)((tmp,next)=>下拉列表中的值(tmp,next))
是短版本
def drop_Val_In_List[String](ls: Seq[String], value: String): Seq[String] = {
val index = ls.indexOf(value) //index is -1 if there is no matc
if (index < 0) {
ls
} else if (index == 0) {
ls.tail
} else {
// splitAt keeps the matching element in the second group
val (a, b) = ls.splitAt(index)
a ++ b.tail
}
}
def drop_Val_List_In_List[String](ls: Seq[String], in_ls: Seq[String]): Seq[String] = {
var tmp_ = ls
//println(tmp.getClass)
for(x <- in_ls ){ // should work without var x
println(x)
tmp_ = drop_Val_In_List(tmp_, x)
}
tmp_
}
val KeepCols = Seq("ctn", "type", "month", "car", "road")
val tmp = drop_Val_List_In_List(KeepCols, List("ctn", "type", "month"))// Start writing your ScalaFiddle code here
println(tmp)
// Start writing your ScalaFiddle code here