如何在Array.sliding中跟踪索引

时间:2017-06-17 20:44:13

标签: arrays scala sequence sliding

在scala中的sliding上使用Array函数时,根据原始数组跟踪滑动子数组索引的正确方法是什么?

// Initialize some data
val bigArray = List(2, 3, 4, 2, 3, 6, 8, 4, 5).toArray
val n:Int = 5

// Slide through the big array
for (smallArray <- bigArray.sliding(n)) {
  val thirdValue:Int = smallArray(3)
  val k = (bigArray zip smallArray) lastIndexWhere { case (x, y) => x < y }
  if (bigArray(k+1) >= thirdValue) {
    println(bigArray.toList.toString + 
      " has "
      + bigArray(k+1)
      + " >= " 
      + thirdValue 
      + " in "
      + smallArray.toList.toString
      + " at index "
      + k+1)
  }

现在我知道了

val k = (bigArray zip smallArray) lastIndexWhere { case (x, y) => x < y }

不正确。根据原始smallArray跟踪bigArray的位置的正确方法是什么?

我得到的是

List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 2 >= 2 in List(2, 3, 4, 2, 3) at index -11
List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 6 >= 3 in List(3, 4, 2, 3, 6) at index 41
List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 6 >= 6 in List(4, 2, 3, 6, 8) at index 41
List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 6 >= 4 in List(3, 6, 8, 4, 5) at index 41

我需要

List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 6 >= 2 in List(2, 3, 4, 2, 3) at index 5
List(2, 3, 4, 2, 3, 6, 8, 4, 5) has 8 >= 3 in List(3, 4, 2, 3, 6) at index 6

更新

我无法map使用来自tuple2(Int, Int)的{​​{1}}来接受答案,但提到head让我在那里,所以我接受了有用的答案。这就是我最终的结果:

zipWithIndex

产生:

for (zipArray <- bigArray.slice(0, bigArray.length - 1).zipWithIndex.sliding(n)) {
  val j = zipArray.head._2
  val k = zipArray.last._2
  val smallArray = bigArray.slice(j, k)
  val thirdValue:Int = smallArray(3)
  if (bigArray(k+1) >= thirdValue) {
    println(bigArray.toList.toString +
      " has "
      + bigArray(k+1)
      + " >= "
      + thirdValue
      + " in "
      + smallArray.toList.toString
      + " at index "
      + (k+1) )
  }
}

1 个答案:

答案 0 :(得分:2)

您可以在<form class="form-horizontal well w3-center" style="max-width: 850px; width: 100%; margin: 0 auto;"> <div class="control-group"> <label class="control-label">A</label> <div class="controls"> <input id="A" type="text" name="A" class="ratecalcfield"> </div> </div> <div class="control-group"> <label class="control-label">B</label> <div class="controls"> <input id="B" type="text" name="B" class="ratecalcfield"> </div> </div> <div class="control-group"> <label class="control-label">C</label> <div class="controls"> <input id="C" type="text" name="C" class="ratecalcfield"> </div> </div> <div class="control-group"> <label class="control-label">X Value</label> <div class="controls"> <input id="X" type="text" name="x" class="ratecalcfield"> </div> </div> <div class="control-group"> <label class="control-label">Y Value</label> <div class="controls"> <input id="Y" type="text" name="y" class="ratecalcfield"> </div> </div> <div class="control-group w3-margin-top"> <div class="controls"> <input type="button" class="w3-button w3-blue w3-padding-large" style="max-width: 200px; width: 100%;" onclick="cmdCalc_Click()" value="Calculate" name="cmdCalc"> </div> </div> </form>之前使用zipWithIndex

sliding