Scala 语法入门

栏目: Scala · 发布时间: 7年前

1. for 循环,while 循环

for(i <- 1 to 3; j <- 1 to 3) println(i * j)
for(i<-1 to 3;j<-1 to 3 if i != j) print(f"${10*i+j}%3d")

2. 函数

//参数后面跟类型
def sum(a : Int, b : Int) : Int = {return a + b}
var sumFun = (a:Int, b:Int) => (a + b)
//默认值
def sum(a : Int = 3, b : Int = 4) : Int = {return a + b}
//可变的参数数量
def sum(args : Int*) = {var result = 0; for(arg <- args) result += arg; result}

3.数组

val intValueArr = new Array[Int](3)
//用()来根据下标取值
intValueArr(0) = 4
//会自动判断类型
val intValueArr = Array(12,45,33)

3.1 可变数组

import scala.collection.mutable.ArrayBuffer
val b = ArrayBuffer[Int]()
//尾部增加一个
b += 1
//增加1,2
b += (1,2)
//尾部增加数组
b ++= Array(4,4)
//去掉最后3个
b.trimEnd(3)
//第二个位置插入4
b.insert(2, 4)
//第3个位置插入4,5
b.insert(3, 4, 5)
//移除第二个
b.remove(2)
//转成array
 b.toArray
4162

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

Lincoln Stein、Doug MacEachern / O'Reilly Media, Inc. / 1999-03 / USD 39.95

Apache is the most popular Web server on the Internet because it is free, reliable, and extensible. The availability of the source code and the modular design of Apache makes it possible to extend Web......一起来看看 《Writing Apache Modules with Perl and C》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具