Scala 为什么这会导致;类型不匹配“;?

Scala 为什么这会导致;类型不匹配“;?,scala,Scala,我猜您正在使用Map来维护数组中每个数字的出现次数。然后返回高发生率计数 Solution.scala:34: error: type mismatch; found : Int required: (Int, Int) myMap += ( value, 1 ) ^ Solution.scala:34: error: type mismatch; found : Int(1) required

我猜您正在使用
Map
来维护数组中每个数字的出现次数。然后返回高发生率计数

Solution.scala:34: error: type mismatch;
 found   : Int
 required: (Int, Int)
                myMap += ( value, 1 )
                           ^
Solution.scala:34: error: type mismatch;
 found   : Int(1)
 required: (Int, Int)
                myMap += ( value, 1 )
                                  ^
Solution.scala:36: error: type mismatch;
 found   : Int
 required: (Int, Int)
                myMap += ( value, ++total )
                           ^
Solution.scala:36: error: not found: value ++
                myMap += ( value, ++total )
                                  ^
four errors found
但在Scala世界中,可变的东西确实是不受欢迎的(除非需要)。这里是第三种方法,不使用任何可变的

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird1(sightIncidents: Array[Int]): (Int, Int) = {
  // mutable reference but immutable map
  var map = scala.collection.immutable.Map.empty[Int, Int]

  sightIncidents.foreach(i => {
    val count = map.getOrElse(i, 0)
    val updatedCount = count + 1
    // since map is immutable, we create new map
    val newMapWithUpdatedCount = map + (i -> updatedCount)
    // we mutate the reference to point to new map
    map = newMapWithUpdatedCount
  })

  map.maxBy(_._2)
}

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird2(sightIncidents: Array[Int]): (Int, Int) = {
  // immutable reference but mutable map
  val map = scala.collection.mutable.Map.empty[Int, Int]

  sightIncidents.foreach(i => {
    val count = map.getOrElse(i, 0)
    val updatedCount = count + 1
    // we mutate the map by putting the updatedCount
    map += (i -> updatedCount)
    // it is not the += operator like other languages
    // it is actually a method
    // map.+=(i -> updatedCount)
  })

  map.maxBy(_._2)
}
或者,你可以选择最简单的解决方案

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird3(sightIncidents: Array[Int]): (Int, Int) = {
  // mutable reference and mutable map
  val emptyMap = scala.collection.immutable.Map.empty[Int, Int]

  val map = sightIncidents.foldLeft(emptyMap)({
    case (accMap, i) =>
      val count = accMap.getOrElse(i, 0)
      val updatedCount = count + 1
      val newMapWithUpdatedCount = map + (i -> updatedCount)
      newMapWithUpdatedCount
  })

  map.maxBy(_._2)
}

我猜您正在使用
Map
来维护数组中每个数字的出现次数。然后返回高发生率计数

Solution.scala:34: error: type mismatch;
 found   : Int
 required: (Int, Int)
                myMap += ( value, 1 )
                           ^
Solution.scala:34: error: type mismatch;
 found   : Int(1)
 required: (Int, Int)
                myMap += ( value, 1 )
                                  ^
Solution.scala:36: error: type mismatch;
 found   : Int
 required: (Int, Int)
                myMap += ( value, ++total )
                           ^
Solution.scala:36: error: not found: value ++
                myMap += ( value, ++total )
                                  ^
four errors found
但在Scala世界中,可变的东西确实是不受欢迎的(除非需要)。这里是第三种方法,不使用任何可变的

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird1(sightIncidents: Array[Int]): (Int, Int) = {
  // mutable reference but immutable map
  var map = scala.collection.immutable.Map.empty[Int, Int]

  sightIncidents.foreach(i => {
    val count = map.getOrElse(i, 0)
    val updatedCount = count + 1
    // since map is immutable, we create new map
    val newMapWithUpdatedCount = map + (i -> updatedCount)
    // we mutate the reference to point to new map
    map = newMapWithUpdatedCount
  })

  map.maxBy(_._2)
}

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird2(sightIncidents: Array[Int]): (Int, Int) = {
  // immutable reference but mutable map
  val map = scala.collection.mutable.Map.empty[Int, Int]

  sightIncidents.foreach(i => {
    val count = map.getOrElse(i, 0)
    val updatedCount = count + 1
    // we mutate the map by putting the updatedCount
    map += (i -> updatedCount)
    // it is not the += operator like other languages
    // it is actually a method
    // map.+=(i -> updatedCount)
  })

  map.maxBy(_._2)
}
或者,你可以选择最简单的解决方案

/**
  * @param sightIncidents Each Int "i" in the corresponds to one sighting of bird species "i"
  * @return tuple of bird species with the count of occurences for the most sighted bird
  */
def mostSightedMigratoryBird3(sightIncidents: Array[Int]): (Int, Int) = {
  // mutable reference and mutable map
  val emptyMap = scala.collection.immutable.Map.empty[Int, Int]

  val map = sightIncidents.foldLeft(emptyMap)({
    case (accMap, i) =>
      val count = accMap.getOrElse(i, 0)
      val updatedCount = count + 1
      val newMapWithUpdatedCount = map + (i -> updatedCount)
      newMapWithUpdatedCount
  })

  map.maxBy(_._2)
}

尝试这样做
myMap+=(值->1)
而不是
myMap+=(值,1)

您甚至可以改进这段代码,如

def mostSightedMigratoryBird2(sightIncidents: Array[Int]): (Int, Int) =
  sightIncidents
    .groupBy(i => i)
    .map({ case (i, group) => (i, group.size) })
    .maxBy(_._2)
for(值总计。折叠(1)(+1))
}
---或---

for(值(myMap.getOrElse(值,0)+1))
}

像这样尝试
myMap+=(值->1)
而不是
myMap+=(值,1)

您甚至可以改进这段代码,如

def mostSightedMigratoryBird2(sightIncidents: Array[Int]): (Int, Int) =
  sightIncidents
    .groupBy(i => i)
    .map({ case (i, group) => (i, group.size) })
    .maxBy(_._2)
for(值总计。折叠(1)(+1))
}
---或---

for(值(myMap.getOrElse(值,0)+1))
}

Scala没有用于递增的
i++
++i
概念。问题仍然存在?Scala没有用于递增的
i++
++i
概念。问题仍然存在?太好了!!谢谢你所做的一切。你对我如何提高Scala技能有什么建议吗?@AugustineOgundimu很抱歉打扰你,但作为一个几年前从Java转向Scala的人,我想给你我的建议:首先,如果你没有参加任何在线基础课程(如Coursera课程)或阅读任何好的介绍书(就像scala为不耐烦的人准备的,或者用scala编程)-熟悉该语言并向自己介绍常见模式。第二,禁止自己使用易变性-我知道,学习某些东西的唯一方法是通过这样做。因此,如果你想学习FP do FP,第一步是不变性和纯度。@Augustineongundimu Scala与Java有很大不同。最重要的是在倾斜过程中,Scala将“强制”使用
不变性
,这是激励自己理解Scala的最简单方法。如果你不这样做,你将继续以类似java的方式编写Scala,这将导致一段漫长且充满bug的旅程。太好了!!感谢你所做的一切。你对我如何提高Scala技能有什么建议吗?@AugustineOgundimu很抱歉打扰您,但是,作为一个几年前从Java转向Scala的人,我想给您我的建议:首先,如果您没有参加任何在线基础课程(如Coursera课程),或者没有读过任何好的介绍书(如《不耐烦的人》的Scala或Scala编程)-熟悉该语言并向自己介绍常见模式。第二,禁止自己使用易变性-我知道,学习某些东西的唯一方法是通过这样做。因此,如果你想学习FP do FP,第一步是不变性和纯度。@Augustineongundimu Scala与Java有很大不同。最重要的是在学习Scala的过程中,用
不变性“强迫”你自己,这是激励你自己理解Scala的最简单的方法。如果你不这样做,你将继续以一种非常类似java的方式编写Scala,这将导致一个非常漫长且充满bug的旅程。