scala语言入门(三)scala集合
【摘要】 一、定长数组和变长数组
package com.dzx.listPack import scala.collection.mutable.ArrayBuffer object ArrayApp extends App { //1.定长数组 val a = new Array[String](5) a.length a(0) = "hello" printl...
一、定长数组和变长数组
-
package com.dzx.listPack
-
-
import scala.collection.mutable.ArrayBuffer
-
-
object ArrayApp extends App {
-
-
//1.定长数组
-
val a = new Array[String](5)
-
-
a.length
-
-
a(0) = "hello"
-
-
println(a)
-
println(a(0))
-
-
val b = Array("hadoop", "hello", "spark", "hdfs", "mapredurce")
-
-
println(b(0), b(1))
-
-
val c = Array(2, 3, 4, 5, 6, 7, 8, 9)
-
-
println(c.sum) //求和
-
println(c.max) //最大值
-
println(c.mkString) //转为string
-
println(c.mkString(",")) //以逗号隔开的string
-
println(c.mkString("<", ",", ">")) // 开始和结束分别是< >
-
-
//2.变长数组
-
val d = ArrayBuffer[Int]()
-
-
//追加元素
-
d += 1
-
d += 2
-
-
//追加多个元素
-
d += (3, 4, 5)
-
-
//直接追加一个数组
-
d ++= Array(6, 7, 8)
-
-
//在index 为0 的位置添加一个 0元素
-
d.insert(0, 0)
-
-
println(c.mkString(","))
-
-
//移除
-
d.remove(1)
-
d.remove(0, 3)
-
-
//从末尾移除
-
d.trimEnd(2)
-
-
d.toArray
-
-
for (i <- 0 until d.length) {
-
println(d(i))
-
}
-
-
for (ele <- d) {
-
println(ele)
-
}
-
-
for (i <- (0 until d.length).reverse) {
-
println(d(i))
-
}
-
}
二、定长list,set ,变长list ,set 以及 模式匹配
-
object ListApp extends App {
-
-
//list 有序的,可以重复的
-
-
//定长list
-
val l = List(11, 2, 3)
-
println(l.head)
-
println(l.tail)
-
-
-
Nil // 等于 List(),空的不可变长的集合
-
val l2 = 1 :: Nil
-
val l3 = 2 :: l2
-
val l4 = 1 :: 2 :: 3 :: 4 :: Nil
-
-
//变长list
-
val l5 = scala.collection.mutable.ListBuffer[Int]()
-
-
l5 += 2
-
l5 += (3, 4, 6)
-
-
-
l5 ++= List(6, 7, 8, 9)
-
-
l5 -= 2
-
-
l5 -= 3
-
l5 -= (1, 4)
-
l5 --= List(5, 6, 7, 8)
-
-
println(l5)
-
-
l5.isEmpty
-
l5.head
-
l5.tail
-
l5.tail.head
-
-
def sum(nums: Int*): Int = {
-
if (nums.length == 0) {
-
0
-
} else { //递归
-
nums.head + sum(nums.tail: _*) //_* 将nums.tail转换为 可变参数
-
}
-
}
-
-
println(sum())
-
println(sum(1, 2, 3, 4))
-
-
-
//set 无序的,不重复的
-
-
val s = Set(1, 2, 2, 1, 4, 3)
-
println(s) //Set(1, 2, 4, 3)
-
-
val s1 = scala.collection.mutable.Set[Int]()
-
-
s1 += 1
-
s1 += (11, 2, 3)
-
s1 ++= Set(4, 5, 6)
-
-
-
//模式匹配
-
// match case 类似于 java 中的 switch case
-
var names = Array("a", "b", "c");
-
val name = names(Random.nextInt(names.length))
-
-
name match {
-
case "a" => println("季老师")
-
case "b" => println("波老师")
-
case _ => println("真不知道你们在说什么。。。")
-
}
-
-
//case 不仅仅 可以匹配一个值,还可以加一个if条件进行过滤判断
-
//字符串的模式匹配
-
def judgeGrade(grade: String, name: String) = {
-
grade match {
-
case "A" => println("Excellent...")
-
case "B" => println("Good ...")
-
case "C" => println("just so so...")
-
case _ if (name == "lisi") => println(name + ",you are a good boy")
-
case _ => println("you need work harder...")
-
}
-
}
-
-
judgeGrade("f", "lisi")
-
-
//数组的模式匹配
-
def greeting(array: Array[String]) = {
-
array match {
-
case Array("zhangsan") => println("hi:zhangsan")
-
case Array(x, y) => println("hi :" + x + "," + y)
-
case Array("zhangsan", _*) => println("hi zhangsan and other friends ...")
-
case _ => println("hi : everybody ...")
-
}
-
}
-
-
greeting(Array("zhangsan"))
-
-
//list的模式匹配
-
def listMatch(list: List[String]): Unit = {
-
list match {
-
case "zhangsan" :: Nil => println("hi zhangsan")
-
case x :: y :: Nil => println("hi:" + x + "," + y)
-
case "zhangsan" :: tail => println("hi :zhangsan and other friends")
-
case _ => println("hi :everybody....")
-
}
-
}
-
-
listMatch(List("lisi"))
-
-
-
//类型的模式匹配
-
def typeMatch(obj: Any): Unit = {
-
obj match {
-
case x:Int =>println("Int")
-
case s:String =>println("String")
-
case m:Map[_,_]=>m.foreach(println)
-
case _ =>println("other type")
-
}
-
}
-
-
typeMatch(Map("name"->"pk"))
-
-
-
//sacla 的异常处理
-
-
-
try {
-
-
-
var i = 10 / 0
-
println(i)
-
}catch{
-
case e:ArithmeticException=>println("除数不能为0")
-
case e:Exception =>println(e.getMessage())
-
}finally {
-
//释放资源,一定能执行
-
}
-
-
-
-
-
-
//case class 的模式匹配
-
class Person
-
case class CTO( name:String,floor:String ) extends Person
-
case class Employee( name:String,floor:String ) extends Person
-
case class Other( name:String) extends Person
-
-
def caseClassMatch(person: Person): Unit ={
-
person match {
-
case CTO(name,floor)=> println("cto name is "+name)
-
case Employee(name,floor) =>println("employee name is "+name)
-
case Other(name) =>println("other name is "+name)
-
}
-
}
-
-
caseClassMatch(CTO("ctO","5"))
-
-
}
文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_31905135/article/details/85072111
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)