Scala For循环和元组树集的类型不匹配错误

Scala For循环和元组树集的类型不匹配错误,scala,Scala,我是一名新的Scala程序员,遇到了一个问题。我正在编写一个函数来获取的数目。当我试图从for循环(在If/Else语句中封装的底部附近)获取值时,它会给我一个类型不匹配错误。我期待一个带有字符串和Int(scala.collection.mutable.TreeSet[(String,Int)])的树集元组,但它说它返回一个单位 以下是我的说明,我相信如果我解决了这个错误,它应该会起作用 注意:返回类型是元组的树集 其类型为(String,Int)。例如,给定人员列表(“tom”, “安”、“

我是一名新的Scala程序员,遇到了一个问题。我正在编写一个函数来获取的数目。当我试图从for循环(在If/Else语句中封装的底部附近)获取值时,它会给我一个类型不匹配错误。我期待一个带有字符串和Int(scala.collection.mutable.TreeSet[(String,Int)])的树集元组,但它说它返回一个单位

以下是我的说明,我相信如果我解决了这个错误,它应该会起作用

注意:返回类型是元组的树集 其类型为(String,Int)。例如,给定人员列表(“tom”, “安”、“罗伯”、“罗伯”、“安”、“汤姆”、“帕特”、“罗伯”、“帕特”、“汤姆”)您的功能应该是 返回树集((汤姆,3),(罗布,3))。注意:最大的骗子有一条领带, 您必须返回集合中所有最大的骗子。仅显示姓名的列表 一次返回一个空树集。“

***函数“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))
def biggestCrooks(人物:List[String]):TreeSet[(String,Int)]={
val crooksListed=crooks(people).toList//运行我的一个函数,该函数为我提供
//一次以上犯罪的罪犯名单
var numCounted=new Array[Int](crokslisted.size)//保存人名次数的数组
//出现
对于(nameIndex 1){//使用的JIC,有多个numValue实例

对于(i请记住,scala中的每个语句都是一个表达式,每个表达式都有一个返回类型,应该返回一些内容。在示例if-else语句的第一个分支中,返回nothing,在scala中,它表示单位类型:

if(numValues>1){//Used JIC有多个numValues实例
为了
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
}