Scala中的嵌套循環

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540662.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540662.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540662.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python基礎-字典

字典 # 字典是python基本數據結構之一&#xff0c;相對于列表和元組&#xff0c;他是無序的&#xff0c;每次輸出都打亂了順序&#xff0c;沒有下標hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算術運算符_SQL中的算術運算符

sql算術運算符SQL | 算術運算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的數字運算管理員來表示特定的…

HDU 6188 Duizi and Shunzi

棧。 將數字排序后&#xff0c;一個一個壓入棧。如果棧頂兩個元素形成了對子&#xff0c;那么$ans1$&#xff0c;彈出棧頂兩個元素&#xff1b;如果棧頂三個元素形成了順子&#xff0c;那么$ans1$&#xff0c;彈出棧頂三個元素。 #include<bits/stdc.h> using namespace …

php 單例模式有什么缺點_PHP的完整形式是什么?

php 單例模式有什么缺點PHP&#xff1a;超文本預處理器 (PHP: Hypertext Preprocessor ) PHP is an abbreviation of Hypertext Preprocessor, earlier called Personal Home Page. PHP is extensively used HTML-embedded, open-source server-side scripting language create…

Myeclipse有關的問題

Myeclipse配置問題 1.行數顯示 window ----preference----General-----Editors-----TextEditors----show line numbers 2.編碼設置 window ---preference----workspace-----設置 3.jsp編碼設置 window ---preference----myeclipse------Files And Editors------jsp 4.jsp的視圖…

weak-to-strong-generalization始終比母體更智能的人工智能,能否被它的母體所監管supervision,從而變的更強

正如supervison這個詞&#xff0c;就像就是母親對孩子的超級super愿景vision&#xff0c;比母親更聰明更強&#xff0c;也就意味著要按照母親期望的那樣成長&#xff0c;不合理的行為要能夠糾正supervison。 一代比一代強&#xff0c;一代比一代好。 弱模型監督能否激發出更強…

最小跳數

Description: 描述&#xff1a; This problem is a standard interview problem which has been featured in interview rounds of Adobe, Amazon, Oyo rooms etc. 此問題是標準的采訪問題&#xff0c;已在Adobe&#xff0c;Amazon&#xff0c;Oyo房間等的采訪回合中出現。 P…

《Web安全之機器學習入門》一 第3章 機器學習概述

第3章 機器學習概述機器學習的概念非常多&#xff0c;從有監督到無監督&#xff0c;從聚類到回歸&#xff0c;從淺層學習到深度學習&#xff0c;從準確率到召回率&#xff0c;它們究竟是什么意思呢&#xff1f;本章將介紹最主要的幾個概念。不少機器學習初學者甚至包括業內老司…

ue 抗鋸齒 渲染序列失靈_最大的鋸齒形序列

ue 抗鋸齒 渲染序列失靈Problem statement: 問題陳述&#xff1a; Given a square matrix of size n x n, find the sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence…

團隊-團隊編程項目作業名稱-成員簡介及分工

成員&#xff1a;祁昊 分工&#xff1a;ui設計&#xff0c;美工&#xff0c;詳細設計。轉載于:https://www.cnblogs.com/qihao10086/p/7496101.html

python身份運算符_Python身份運算符

python身份運算符Identity operators are used to perform the comparison operation on the objects i.e. these operators check whether both operands refer to the same objects (with the same memory location) or not. 身份運算符用于對對象執行比較操作&#xff0c;即…

Oracle-Decode()函數和CASE語句的不同

Oracle-Decode()函數和CASE語句的區別&#xff1a; 具體示例如下&#xff1a; 1.CASE語句&#xff1a; SELECT CASE SIGN(5 - 5) WHEN 1 THEN Is Positive WHEN -1 THEN Is Negative ELSE Is Zero END FROM DUAL; 后臺實現&#xff1a; if (SIGN(5 – 5) 1) { Is Positive; } …

ai智能模式_AI的完整形式是什么?

ai智能模式AI&#xff1a;人工智能 (AI: Artificial Intelligence) AI is an abbreviation of "artificial intelligence", which occasionally called machine intelligence in the field of computer science. It is intelligence made understandable by machines…

centos6.5安裝python3.6

1、下載Python安裝包 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 2、解壓安裝包&#xff1a;tar -xzvf Python-3.6.0.tgz 3、進入安裝包路徑&#xff1a;cd Python-3.6.04、編譯安裝包 注意&#xff1a;prefix參數用于指定將Python安裝在新目錄&#xff…

BE的完整形式是什么?

工學學士 (BE: Bachelor of Engineering) BE is an abbreviation of Bachelor of Engineering. It is a bachelors degree program for under graduation in engineering and the duration of this course is 4 years. It is provided in many countries like India, Canada, S…

史上最詳細Windows版本搭建安裝React Native環境配置

說在前面的話: 感謝同事金曉冰傾情奉獻本環境搭建教程 之前我們已經講解了React Native的OS X系統的環境搭建以及配置&#xff0c;鑒于各大群里有很多人反應在Windows環境搭建出現各種問題&#xff0c;今天就特意更新一貼來說明。關于os x環境搭建以及react native入門學習資料…

程序代碼錯誤檢測_錯誤檢測代碼

程序代碼錯誤檢測錯誤檢測代碼 (Error Detecting Codes) A group of bits is known as words, and these words move as an entity from one block to another in the digital system. While moving from one part to another within the system via transmission media, the b…

Web瀏覽器端通過https 使用mqtt通訊

做的產品簡介 這次需要做一個web端的上課平臺&#xff0c;有音視頻通訊&#xff0c;有白板(畫板)功能&#xff0c;有文字通訊等。技術點 音視頻通訊需要走Webrtc需要跟ios, android, windows, mac 客戶端互聯互通一般通訊通過mqtt協議MQTT簡介 MQTT&#xff08;Message Queuing…

vga顯示模式_VGA的完整形式是什么?

vga顯示模式VGA&#xff1a;視頻圖形陣列 (VGA: Video Graphics Array) VGA is an abbreviation of "Video Graphics Array". VGA是“視頻圖形陣列”的縮寫 。 It is a three-row 15-pin DE-15 connector display hardware developed by IBM in 1987. It was first …

【iCore4 雙核心板_FPGA】例程十一:FSMC總線通信實驗——獨立地址模式

實驗原理&#xff1a; STM32F767上自帶FMC控制器&#xff0c;本實驗將通過FMC總線的地址獨立模式實現STM32與FPGA 之間通信&#xff0c;FPGA內部建立RAM塊,FPGA橋接STM32和RAM塊&#xff0c;本實驗通過FSMC總線從STM32向 RAM塊中寫入數據&#xff0c;然后讀取RAM出來的數據進行…