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.mzph.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出来的数据进行…