当前位置:网站首页>Scala基础
Scala基础
2022-07-19 05:58:00 【钟钟终】
基础知识练习:
关键字练习
import scala.util.control.Breaks._
object test01 {
def main(args: Array[String]): Unit = {
var s="hello world" //字符串操作
println(s.indexOf(" "))
println(s.reverse)
var tuple1=("hello","world","hhh") //元组
println(tuple1)
val score=78; //条件控制语句
if(score>=90)
println("A")
else
println("B")
var i=15 //while循环语句
while(i<=20){
println(i)
i+=1
}
for(i<-1 to (11,2)) //for循环
println("to "+i)
for(i<-1 until (11,2))
println("until "+i)
// for(i<-1 to 5){
// if(i>=3) {
// break
// }
// println("aaa")
// }
for(i<-1 to 40 if(i%5==0);if(i%4==0))
println(i)
}
}
集合使用
Scala中的集合分为可变集合(mutable)和不可变集合(immutable)。默认创建不可变集合,也是scala推崇的。
数组
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks._
object test01 {
def main(args: Array[String]): Unit =
{
val ms=Set(1,2,3)
println(ms)
//定长数组
val a1=Array("first","second")
val a2=new Array[Int](10)
//变长数组ArrayBuffer
val s=ArrayBuffer[String]()
s+="hello"
s+=("world","hehehe")
s++=List("to","for")
s.trimEnd(1)
for(i<-0 to s.length-1) //数组的遍历
println(s(i))
var a=ArrayBuffer(1,1,2)
a.insert(0,666)
a.insert(0,7,8,9)
a.remove(0,2)
for(i<-0 to s.length-1)
println(a(i))
a.toArray //转化为定长数组
a.toBuffer //转化为ArrayBuffer
for(i<- a) //推荐的遍历方式
printf("%d ",i)
println()
var a3=for(i<- a if i>1) yield i*2 //生成数组
for(i<- a3)
printf("%d ",i)
println()
println(a3.mkString("<",",",">"))
}
}
列表
import scala.collection.immutable.Nil.zip
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks._
object test01 {
def main(args: Array[String]): Unit =
{
val l=List(1,2,3,4,5,6,7,8,9)
println(l.head) //取第一个元素
println(l.tail) //除了第一个元素
println(l.tail.head) //取第二个元素
println(l.init) //去除最后一个元素
println(l.last) //取出最后一个元素
println(l.reverse) //倒置
println(l.drop(3)) //丢弃前三个元素
println(l.take(3)) //取出前三个元素
//zip操作
// val nums=(1,2,3,4)
// val chars=('1','2','3','4')
// nums zip chars
// println(l.isEmpty)
val list1=List(List(1,2,3),List(4,5,6),List(67,8,9))
println(list1.flatten)
}
}
集合(Set)、映射(Map)、队列(Queue)、栈(Stack)
import scala.collection.mutable.Stack
import scala.collection.immutable.Nil.zip
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks._
object test01 {
def main(args: Array[String]): Unit =
{
//集合
val s1=Set(8,1,3) //遍历顺序随机
val s2=s1+6
for(i<- s2)
println(i)
val s3=scala.collection.mutable.LinkedHashSet(5,6,7)
//映射
val m1=Map("zhong"->1,"zhang"->2) //默认创建不可变map
val m2=scala.collection.mutable.Map("zhong"->1,"zhang"->2)
m2.put("li",3)
for(i<-m2)
println(i)
m2.foreach(e=>{
val (k,v)=e;println(k+":"+v)})
println("--------------")
m2.foreach(e=>println(e._1,e._2))
m2.get("zhong")
m2.contains("zhong")
//队列
var queue=scala.collection.mutable.Queue(1,2,3,4)
queue.dequeue()
queue.enqueue(1)
queue++=List(7,8,9)
while(!queue.isEmpty)
printf("%d ",queue.dequeue())
println()
//栈
var stack=new Stack[Int]
stack.push(1)
stack.push(2)
stack.push(3)
stack.pop()
stack.top
while(!stack.isEmpty)
printf("%d ",stack.pop())
println()
}
}
边栏推荐
- Self controlled and next city! Release of the first domestic artiq architecture quantum computing measurement and control system
- 用float特性填满整个正方形
- 8day
- Comparator sorted pit
- 9day
- GC tuning principle of JVM (10)
- GC tuning principle of JVM (13)
- 二叉树的链式存储结构
- 19day
- JDBC连接MySQL Loading class `com.mysql.jdbc.Driver‘. This is deprecated
猜你喜欢
随机推荐
Apache impala 4.1 overview
【MySQL数据库】常见知识点总结
flex布局简介
Kubernetes technology and Architecture (I)
Self controlled and next city! Release of the first domestic artiq architecture quantum computing measurement and control system
Codeforces Round #807 (Div. 2)(A.B.C)
20day
Kubernetes highly available API server
如何在Apache JIRA中搜索issue
ikbc键盘win键失效的解决方法
Modelsim reported an error "instance of '* * * *' failed. the design unit was not found."
playwright教程(二)适合小白
Kubernetes technology and Architecture (IV)
13day
JS object properties
10day
Understanding of blocking assignment and non blocking assignment in FPGA
GC tuning principle of JVM (VIII)
8day
GC tuning principle of JVM (13)