简体   繁体   English

Scala无限循环

[英]Scala infinite loop

So I am new to scala and from what I've found online, scala does not update variable like other languages do.所以我是 scala 的新手,从我在网上找到的内容来看,scala 不会像其他语言那样更新变量。 I am trying to change a variable in the while loop but it seems like it is not changing.我试图在 while 循环中更改一个变量,但它似乎没有改变。 I have a mutable ArrayBuffer that is holding key,value pairs and is declared like:我有一个可变的 ArrayBuffer,它保存键值对,声明如下:

val array1 = mutable.ArrayBuffer[Option[IndexedSeq[(K,V)]]]()

It is storing sorted arrays based on the "K" value which is always an int.它根据始终为 int 的“K”值存储排序数组。 I am trying to loop though the layers of array1 by doing:我试图通过执行以下操作来遍历array1的层:

var i=0
var counter = 0
while(array1(i).isDefined){
    counter += 1
    i += 1}

However, this results in an infinite loop annd I suspect i is not changing and I dont know why.然而,这会导致无限循环,我怀疑i没有改变,我不知道为什么。

You'll do well to forget a lot of the low-level C paradigms when you're working in a high-level language like Scala.当您使用像 Scala 这样的高级语言工作时,最好忘记许多低级 C 范式。 It's just going to steer you wrong.它只会误导你。

If you have an iterable thing (such as an ArrayBuffer , a List , or anything else that vaguely implies a collection of things), you can iterate over it with如果你有一个可迭代的东西(比如一个ArrayBuffer ,一个List ,或者任何其他模糊暗示一个事物集合的东西),你可以迭代它

myArray.foreach { element => ... }

or, if you want to build a new collection out of the result或者,如果您想从结果中构建一个新集合

val myNewArray = myArray.map { element => ... }

To remove some elements based on an arbitrary condition, consider要根据任意条件删除一些元素,请考虑

val myNewArray = myArray.filter { element => ... }

The "always throw it in a while / for loop" approach is very C-centric, and in Scala we have tons of higher-level, more expressive tools to do the work we want to do. “始终将其放入while / for循环”方法非常以 C 为中心,在 Scala 中,我们有大量更高级、更具表现力的工具来完成我们想做的工作。 Scala still has a while loop, but you seldom need to use it, simply because there are so many built-in functions that express the common looping paradigms for you. Scala 仍然一个while循环,但你很少需要使用它,因为有太多的内置函数可以为你表达常见的循环范式。

A functional programmer can look at函数式程序员可以看看

val myNewArray = myArray.map { _ + 1 }

and immediately see "aha, we're iterating and making a new array by adding one to each element", and it breaks the problem down a ton.并立即看到“啊哈,我们正在迭代并通过向每个元素添加一个来创建一个新数组”,它将问题分解为一吨。 The "equivalent" code in C-style would be something like C 样式中的“等效”代码类似于

val myNewArray = ArrayBuffer()
var i = 0
while (i < myArray.size) {
  myNewArray += (myArray(i) + 1)
  i += 1
}

The basic concept is still the same: "add one to each element", but there's a ton more to parse here to get to the actual idea of the code.基本概念仍然是相同的:“添加一个到每个元素”,但有一多在这里解析得到的代码的实际想法。 This code doesn't immediately read "add one to elements of a list".此代码不会立即读取“向列表元素添加一个”。 It reads "okay, we're making an array and using a local variable, and then we're doing something several times that looks like it's adding one and..."它写着“好吧,我们正在创建一个数组并使用一个局部变量,然后我们做了几次看起来像是添加一个和......”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM