scala - For 循环和元组树集的类型不匹配错误
问题描述
我是一个新的 Scala 程序员,我遇到了一个问题。我正在编写一个函数来获取的数量当我尝试从我的 for 循环中获取值时(靠近封装在 If/Else 语句中的底部),它给了我一个类型不匹配错误。我期待一个带有 String 和 Int 的 TreeSet 元组(scala.collection.mutable.TreeSet[(String, Int)]),但它说它返回一个 Unit。
这是我的说明,我相信如果我解决了这个错误,它应该可以工作。
"注意:返回类型是具有 (String, Int) 类型的元组的 TreeSet。例如,给定人员列表 List(“tom”, “ann”, “rob”, “rob”, “ann” , “tom”, “pat”, “rob” “pat”, “tom”) 你的函数应该返回 TreeSet((tom,3), (rob,3))。注意:最大的骗子有一个平局,你必须返回集合中所有最大的骗子。一个名字只出现一次的列表返回一个空的 TreeSet。
***函数“crooks”和“a”都在其他未显示的地方定义。
def biggestCrooks(people: List[String]): TreeSet[(String,Int)] = {
val crooksListed = crooks(people).toList //Runs my one function that gives me the
// list of criminals with more than one offense
var numCounted = new Array[Int](crooksListed.size) // Array to hold the number of times a person name
//appears
for(nameIndex <- 0 until crooksListed.size){ // Counts the number of times a name appears
val counter = people.count(_.equals(crooksListed(nameIndex))) //Stores that name in the array
numCounted(nameIndex) = counter //Stores the num in the index associated with the name
}
var largestValue = numCounted.max // Largest value in the array
val numValues = numCounted.count(_ == largestValue) // Number of times the value appears
var indeces : Set[Int] = Set()
var completeList : TreeSet[(String, Int)] = TreeSet() // Completed list of all names / values
if(numValues > 1){// Used JIC there are multiple instances of numValues
for(i <- 0 until numCounted.length){ //If there are multiple instances, find the indeces of them
if(numCounted(i) == largestValue)
indeces(i) //add the index to the array
}
val wow = indeces.toList //Converts a mutable Set to an immutable list
for(i <- 0 until wow.size){ //iterate through the indeces and associate with names
completeList.map(crooksListed(wow(i)), wow(i)) //Maps the values to the completeList TreeSet
//Supposed to solve problem with mismatched types
}
}
else{
completeList.map(i => (crooksListed(numCounted.indexOf(largestValue)), largestValue)) //Maps the values to the TreeSet
}
}// end biggestCrook
println(biggestCrooks(a))
解决方案
请记住,scala 中的每个语句都是一个表达式,每个表达式都有一个返回类型,并且应该返回一些东西。在 if else 语句的示例第一个分支中返回nothing,在 scala 中它意味着Unit类型:
if(numValues > 1){// Used JIC there are multiple instances of numValues
for(i <- 0 until numCounted.length){ //If there are multiple instances, find the indeces of them
if(numCounted(i) == largestValue)
indeces(i) //add the index to the array
}
val wow = indeces.toList //Converts a mutable Set to an immutable list
for(i <- 0 until wow.size){ //iterate through the indeces and associate with names
completeList.map(crooksListed(wow(i)), wow(i)) //Maps the values to the completeList TreeSet
//Supposed to solve problem with mismatched types
}
// this for loop returns Unit, it just iterates on elements, and you need to add return value for compile
}
else{
completeList.map(i => (crooksListed(numCounted.indexOf(largestValue)), largestValue)) //Maps the values to the TreeSet
// in this place you return mapped cimpleteList as Tree, but be carefull it's empty
}
我认为这个样本可能是这样的:
if (numValues > 1) { // Used JIC there are multiple instances of numValues
for (i <- numCounted.indices) { //If there are multiple instances, find the indeces of them
if (numCounted(i) == largestValue)
indeces(i) //add the index to the array
}
indeces.map(w => crooksListed(w) -> w).foldLeft(TreeSet.empty[(String, Int)]){
case (tree, pair) => tree + pair
}
} else {
completeList.map(i => (crooksListed(numCounted.indexOf(largestValue)), largestValue)) //Maps the values to the TreeSet
}
在这里,我通过 crooksListed 和 wow indeces 成对填充真实分支中的空树,并且该分支像错误分支一样返回 TreeSet。尝试阅读有关表达式的更多信息并尝试使用递归而不是for和while循环。
推荐阅读
- javascript - 制作一个响应式导航栏,内容位于左侧、中间和右侧
- visual-studio-code - textMateRules 的多范围选择
- python - 对矩阵中的特定行数求和
- python - 如何匹配2个字符串列表?
- java - jmeter-maven-plugin - 如何在不下载的情况下使用插件
- angular - 输入绑定的数据在刷新之前一直保留在输入中
- if-statement - Google Sheets Importrange statement in SWITCH
- java - 哪个 Java 库可以帮助将包含形状和 Word 绘图的 MS Word 文档转换为 PDF?
- html - CSS网格 - 将具有不同背景的2列设置为全屏宽度,它们的内容都为联合最大宽度
- android - 如何在 react-native 中从另一个组件访问组件的状态?