【Scala】Scala之Control Structures】的更多相关文章

一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Structures Scala中的控制结构与Java中的颇为类似,但也有所不同,例如,if/then/else控制结构与Java的类似,但是其可以返回值,虽然Java中有三元运算符的特殊语法,但是在Scala中使用if就可以达到同样的效果. val x = if (a) y else z 同样,Scala的try/catch/finally 结构与Java…
Scala之Control Structures 一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Structures Scala中的控制结构与Java中的颇为类似,但也有所不同,例如,if/then/else控制结构与Java的类似,但是其可以返回值,虽然Java中有三元运算符的特殊语法,但是在Scala中使用if就可以达到同样的效果. val x = if (a) y else z 同样,Scala的…
From:http://interactivepython.org/courselib/static/pythonds/Introduction/ControlStructures.html Control Structures As we noted earlier, algorithms require two important control structures: iteration and selection. Iteration while >>> counter = 1…
Control Structures Control structures in R allow you to control the flow of execution of the program, depending on runtime conditions. Common structures are: if, else: testing a condition for: execute a loop a fixed number of times while: execute a l…
Object An object is a type of class that can have no more than one instance, known in object-oriented design as a singleton. Instead of creating an instance with a new keyword, just access the object directly by name. Objects provide similar "static&…
1.List 基础操作 2.Set 基础操作 3. TreeSet 排序Set 4. 拉链操作 5. 流 import scala.collection.immutable.TreeSet import scala.util.Random /** * @author xwolf * @date 2017-04-25 15:26 * @since 1.8 */ object Collection { def main(args:Array[String]):Unit={ list() // set…
1.scala 函数定义 2.scala 高阶函数 3.匿名函数 4.柯里化 import scala.math._ /** * @author xwolf * @date 2017-04-24 9:57 * @since 1.8 */ class Function { // _ 将round 方法转化为函数 val cel = round _ def add(x:Int,y:Int) = x+y //函数 def f(x : => Int) = x.+(3) def m (r : => Do…
1. Regex对象 我们可以使用scala.util.matching.Regex类使用正则表达式.要构造一个Regex对象,使用String类的r方法即可: val numPattern = "[0-9]+".r 如果正则表达式包含反斜杠或引号的话,那么最好使用"原始"字符串语法"""...""": val positiveNumPattern = """^[1-9]\d*…
1. 读取行 读取文件,可以使用scala.io.Source对象的fromFile方法.如果读取所有行可以使用getLines方法: val source = Source.fromFile("/home/xiaosi/exception.txt", "UTF-8") val lineIterator = source.getLines() for(line <- lineIterator){ println(line) } source.close() s…
1. 单例对象 Scala没有静态方法或静态字段,可以使用object来达到这个目的,对象定义了某个类的单个实例: object Account{ private var lastNumber = 0 def newUniqueNumber () = {lastNumber += 1; lastNumber} } 当你在应用程序中需要一个新的唯一账号时,调用Account.newUniqueNumber()即可.对象的构造器在该对象第一次被使用时调用.在本例中,Account的构造器在Accou…