我如何在 kotlin 的 when 表达式中更新 2 个列表变量?
How can i update 2 lists variables in when expression of kotlin?
fun compareWithIntermediate(output: SHR, input: Intpos): CompareResult{
val matched = mutableListOf<String>()
val mismatched = mutableListOf<String>()
val modifiableAttr = mutableListOf<String>()
compareValue(output.a.b.c, input.a.b.c,matched,mismatched)
......
compareValue(output.x.y.z, input.x.y.z,matched,mismatched)
return CompareResult(matched,mismatched)
}
private fun compareValue(outputVal: String, inVal: String, matched:MutableList<String>,
mismatched:MutableList<Diff>){
when(outputVal.compareTo(inVal)==0){
true -> matched.add("abc")
false -> mismatched.add(Diff("abc",outputVal,inVal))
}
}
所以每次调用 compareValue() 时,我都想根据条件将值添加到匹配或不匹配的列表中。
我怎样才能实现这个,因为在什么时候可以实现多个 true/false。我如何根据条件将值添加到相应列表。
枚举是否有助于解决这个问题,而不是在 "when" 中编写多个相同的条件??
请针对您的案例使用更简单的 if
语句:
if (outputVal.compareTo(inVal) == 0) {
matched.add("abc")
// update mismatched list here if need
} else {
mismatched.add(Diff("abc",outputVal,inVal))
// update matched list here if need
}
fun compareWithIntermediate(output: SHR, input: Intpos): CompareResult{
val matched = mutableListOf<String>()
val mismatched = mutableListOf<String>()
val modifiableAttr = mutableListOf<String>()
compareValue(output.a.b.c, input.a.b.c,matched,mismatched)
......
compareValue(output.x.y.z, input.x.y.z,matched,mismatched)
return CompareResult(matched,mismatched)
}
private fun compareValue(outputVal: String, inVal: String, matched:MutableList<String>,
mismatched:MutableList<Diff>){
when(outputVal.compareTo(inVal)==0){
true -> matched.add("abc")
false -> mismatched.add(Diff("abc",outputVal,inVal))
}
}
所以每次调用 compareValue() 时,我都想根据条件将值添加到匹配或不匹配的列表中。
我怎样才能实现这个,因为在什么时候可以实现多个 true/false。我如何根据条件将值添加到相应列表。 枚举是否有助于解决这个问题,而不是在 "when" 中编写多个相同的条件??
请针对您的案例使用更简单的 if
语句:
if (outputVal.compareTo(inVal) == 0) {
matched.add("abc")
// update mismatched list here if need
} else {
mismatched.add(Diff("abc",outputVal,inVal))
// update matched list here if need
}