这里主要给大家展示Scala的基础,比如for循环,函数,数组以及元组和印射,在代码中有注释。
一,for循环:
object For {
def main(args: Array[String]) {
//forumon()
// foruntil()
//forhight()
// forif()
foryeild()
}
def foryeild(): Unit ={
for(i <- 1 to 10) yield i%3
//将生成Vector(1,2,0,1,2,0,1,2,0,1)
}
//在这里if就相当于一个守卫过滤掉一些元素
def forif(): Unit ={
for(i<- 1 to 3;j <- 1 to 3 if i!=j ){
print((10*i+j)+" ")
}
}
def forhight(): Unit ={
for(i<-1 to 3; j <-1 to 3){
print((10*i+j)+" ")
}
}
//until就是在循环的时候取到目标的长度减一
def foruntil(): Unit ={
var aa=new Array[Int](3)
aa(0)=1;
aa(1)=2
aa(2)=3
var sum=0
//0 to aa.length-1 === 0 until aa.length
for(i<- 0 until aa.length){
sum+=aa(i)
}
print(sum)
}
def forumon(): Unit ={
var n=3
var r=1
for(i<- 1 to n){
r=r*i
}
print(r)
}
}
二,函数:
//在Scala中函数的一般不用return返回,最后一个就是返回的值,
//注意在写返回函数时直接用等于就好了,不需要再加unit 加了就是不返回
object Function {
def main(args: Array[String]) {
// var a=abs(2)
// print(a)
// var b=fac(3)
//print(b)
// print(decorate("hello"))
//print(decorate("hello","<<",">>>"))
// val s=sum(1,4,9)
box("Hello")
}
def abs(x:Double) ={
if(x>=0) x else -x
}
def fac(n: Int) ={
var r=1
for(i<- 1 to n)
r=r*i
r//最后一定要写这个返回的值,要不就没有返回的内容
}
//默认参数
def decorate(str:String, left:String="[",right:String ="]")={
left+str+right
}
//变长参数
def sum(args: Int*)={
var result=0
for(arg <- args){
result +=arg
result
}
}
//过程:就是一个函数体没有返回的值,有的可以加Until,不要加=因为没有返回值
def box(s:String){//注意没有 =
var border="-" * s.length +"-\n"
println(border+"|"+s+"|\n"+border)
}
}
三,数组:
object arraytest {
def main(args: Array[String]) {
// arraymeth()
//arrayapply()
// arrayeasy()
// changearray()
bianli()
}
//使用类型参数初始化定长数组
def arraymeth(): Unit ={
val greetStrings =new Array[String](3)
greetStrings(0)="Hello"
greetStrings(1)=","
greetStrings(2)="wolrd!\n"
for(i <- 0 to 2){
print(greetStrings(i))
}
}
//在字符数组中使用apply方法
def arrayapply():Unit={
val greetString =new Array[String](3)
greetString.update(0,"hello")
greetString.update(1,",")
greetString.update(2,"world!\n")
for(i<- 0 to 2){
print(greetString.apply(i))
}
}
//直接在数组上定义字符
def arrayeasy(): Unit ={
val greetStrings =Array("hello",",","world\n")
val greetStringss=Array.apply("hello2",",","world2\n")
for(i<- 0 to 2){
print(greetStrings(i))
}
for(j <- 0 to 2){
//print(greetStringss.apply(j))
print(greetStringss(j))
}
}
//变长数组:数组缓冲
def changearray(): Unit ={
val b=ArrayBuffer[Int]()
b+=1
b+=(1,2,3,4,5)//一次追加多个数据
b++=Array(8,13,21) //追加一个集合
b.trimStart(1)//剔除开头第一个元素
for(i <- 0 until b.length){
print(b(i)+" ")
}
}
//遍历数组如果不需要用到下标
def bianli(): Unit ={
val a=Array(1,2,3,4)
for(elem <- a){
print(elem+" ")
}
}
}
四,印射和元组:
object setandmap {
def main (args: Array[String]) {
// setTest()
maptest()
}
def setTest(): Unit ={
var jestSet=Set("Boeing","Airbus")
jestSet +="Lear"
//println(jestSet.contains("Lear"))
//jestSet.foreach(x=>println(x))
print()
}
def maptest(): Unit ={
val romanNueral =Map(1 ->"hello",2 ->"world",3 ->"!")
// println(romanNueral(1)+"----"+romanNueral.get(1))取单个的值
for(v <- romanNueral.values){
print(v+" ")//迭代输出其值
}
println()
for(k <- romanNueral.keys){
print(k+" ")//迭代输出其key
}
}
}