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
TOP Interview Coding Problems/Challenges
Run-length encoding (find/print frequency of letters in a string)
Sort an array of 0's, 1's and 2's in linear time complexity
Checking Anagrams (check whether two string is anagrams or not)
Relative sorting algorithm
Finding subarray with given sum
Find the level in a binary tree with given sum K
Check whether a Binary Tree is BST (Binary Search Tree) or not
1[0]1 Pattern Count
Capitalize first and last letter of each word in a line
Print vertical sum of a binary tree
Print Boundary Sum of a Binary Tree
Reverse a single linked list
Greedy Strategy to solve major algorithm problems
Job sequencing problem
Root to leaf Path Sum
Exit Point in a Matrix
Find length of loop in a linked list
Toppers of Class
Print All Nodes that don't have Sibling
Transform to Sum Tree
Shortest Source to Destination Path
Comments and Discussions
Ad:
Are you a blogger? Join our Blogging forum.
翻譯自: https://www.includehelp.com/scala/nested-loops-in-scala.aspx