Scala中的嵌套循环 (Nested loop in Scala)
In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested loop in programming is nesting of for loops. As in nesting, the loop body should be simple it is perfect for nesting.
在编程中, 嵌套循环用于初始化或迭代多维数组或打印图案。 Scala提供了一种在编程语言中使用嵌套循环的有效方法。 编程中最常用的嵌套循环是for循环的嵌套。 与嵌套一样,循环体应该很简单,非常适合嵌套。
Nested Loops in Scala, Looping through a 2-D structure required the use of nested loops. The multiple for loop has more than one counter that is used in order to make sure that efficient work is done by the counter.
Scala中的嵌套循环,要在 2D结构中循环,需要使用嵌套循环。 多重for循环使用多个计数器,以确保计数器能够有效完成工作。
For nesting loops, we can place the second loop inside the body of the first loop. This is the traditional way and most used one too. And is done like this,
对于嵌套循环 ,我们可以将第二个循环放在第一个循环的主体内。 这是传统方式,也是最常用的一种方式。 像这样完成
loop1{
loop2{
//code to be executed…
}
}
Example of Nested for loop in Scala:
Scala中的嵌套for循环示例:
object MyClass {
def main(args: Array[String]) {
var i , j = 0;
for(i <- 1 to 2){
for(j <- 1 to 2)
println("(" + i + "," + j + ")")
}
}
}
Output
输出量
(1,1)
(1,2)
(2,1)
(2,2)
In this code two for loops are used to print tuples. The first runs from 1 to 2 and then the second run from 1 to 2 for each iteration of the first loop. For the first iteration of loop 1, loop 2 runs two times printing value 1,1 and 1,2. The same happens for the second iteration of loop 1 that prints 2,1 and 2,2.
在此代码中,两个for循环用于打印元组。 对于第一个循环的每次迭代,第一个从1到2运行,然后第二个从1到2运行。 对于循环1的第一次迭代,循环2运行两次打印值1,1和1,2。 循环1的第二次迭代打印出2,1和2,2时也会发生同样的情况。
In Scala, there is an easier way to make nested loops and this one requires fewer lines of code to be written by the programmer. The one uses multiple loop variable initialization in one body. Making use of this type reduces code length and is quick. This one is coded like this.
在Scala中,有一种更简单的方法来制作嵌套循环,而这种方法需要程序员编写的代码行更少。 一个在一个主体中使用多个循环变量初始化。 使用此类型可减少代码长度,并且速度很快。 这是这样编码的。
loop(coditionvar1 ; condtionvar2){
//body of the loop
}
Example:
例:
object MyClass {
def main(args: Array[String]) {
var i , j = 0;
for(i <- 1 to 2 ; j <- 1 to 2){
println("(" + i + "," + j + ")")
}
}
}
Output
输出量
(1,1)
(1,2)
(2,1)
(2,2)
The output is the same as above (1,1); (1,2);…. This is code in a lesser number of lines and only one block is used that reduces programming errors.
输出与上面的(1,1)相同; (1,2);…。 这是行数较少的代码,并且仅使用一个块来减少编程错误。
Explanation:
说明:
The code works in the same way as it does for two loops. The loop first increments the value of counter 2 (j in this case) until it gets to the maximum possible value (2). Then it increases the counter 1 ( i ) and resets the second counter value to initial value. This continues till the counter 1's value is maxed out.
该代码的工作方式与两个循环的工作方式相同。 循环首先递增计数器2的值(在这种情况下为j ),直到达到最大可能值(2)。 然后,它增加计数器1( i )并将第二个计数器值重置为初始值。 这一直持续到计数器1的值达到最大值为止。
Some other ways to write nested loops:
编写嵌套循环的其他方法:
for{
i
翻译自: https://www.includehelp.com/scala/nested-loops-in-scala.aspx