Spark记录-Scala数组/List/Map/Set
import Array._
import scala.collection.mutable.Set
object DataStructure {
def main(args:Array[String]):Unit={
//数组
var z:Array[String] = new Array[String](3);
var z1 = new Array[String](3);
z(0)="hello";
z1(0)="world";
var z2 = Array("Runoob", "Baidu", "Google");
for(zz<-z2){
println(zz)
}
var myList = Array(1.9, 2.9, 3.4, 3.5)
// 输出所有数组元素
for ( x <- myList ) {
println( x )
}
// 计算数组所有元素的总和
var total = 0.0;
for ( i <- 0 to (myList.length - 1)) {
total += myList(i);
}
println("总和为 " + total); // 查找数组中的最大元素
var max = myList(0);
for ( i <- 1 to (myList.length - 1) ) {
if (myList(i) > max) max = myList(i);
}
println("最大值为 " + max); var myMatrix = ofDim[Int](3,3)
// 创建矩阵
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
myMatrix(i)(j) = j;
}
}
// 打印二维阵列
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
print(" " + myMatrix(i)(j));
}
println();
}
//合并数组
var myList1 = Array(1.9, 2.9, 3.4, 3.5)
var myList2 = Array(8.9, 7.9, 0.4, 1.5)
var myList3 = concat( myList1, myList2)
// 输出所有数组元素
for ( x <- myList3 ) {
println( x )
}
//创建区间数组
var myList4 = range(10, 20, 2)
var myList5 = range(10,20) // 输出所有数组元素
for ( x <- myList4 ) {
print( " " + x )
}
println()
for ( x <- myList5 ) {
print( " " + x )
}
//List列表
// 字符串列表
val site: List[String] = List("Runoob", "Google", "Baidu") // 整型列表
val nums: List[Int] = List(1, 2, 3, 4) // 空列表
val empty: List[Nothing] = List() // 二维列表
val dim: List[List[Int]] =
List(
List(1, 0, 0),
List(0, 1, 0),
List(0, 0, 1)
)
// 字符串列表
val site1 = "Runoob" :: ("Google" :: ("Baidu" :: Nil)) // 整型列表
val nums1 = 1 :: (2 :: (3 :: (4 :: Nil))) // 空列表
val empty1 = Nil // 二维列表
val dim1 = (1 :: (0 :: (0 :: Nil))) ::
(0 :: (1 :: (0 :: Nil))) ::
(0 :: (0 :: (1 :: Nil))) :: Nil /***
* head 返回列表第一个元素
tail 返回一个列表,包含除了第一元素之外的其他元素
isEmpty 在列表为空时返回true
*/
val site2 = "Runoob" :: ("Google" :: ("Baidu" :: Nil))
val nums2 = Nil println( "第一网站是 : " + site2.head )
println( "最后一个网站是 : " + site2.tail )
println( "查看列表 site 是否为空 : " + site2.isEmpty )
println( "查看 nums 是否为空 : " + nums2.isEmpty )
val site3 = "Runoob" :: ("Google" :: ("Baidu" :: Nil))
val site4 = "Facebook" :: ("Taobao" :: Nil) // 使用 ::: 运算符
var fruit = site3 ::: site4
println( "site3 ::: site4 : " + fruit ) // 使用 Set.:::() 方法
fruit = site3.:::(site4)
println( "site3.:::(site4) : " + fruit ) // 使用 concat 方法
fruit = List.concat(site3, site4)
println( "List.concat(site3, site4) : " + fruit ) val site5 = List.fill(3)("Runoob") // 重复 Runoob 3次
println( "site5 : " + site5 ) val num2 = List.fill(10)(2) // 重复元素 2, 10 次
println( "num2 : " + num2 )
// 通过给定的函数创建 5 个元素
val squares = List.tabulate(6)(n => n * n)
println( "一维 : " + squares ) // 创建二维列表
val mul = List.tabulate( 4,5 )( _ * _ )
println( "多维 : " + mul )
//反转
val site6 = "Runoob" :: ("Google" :: ("Baidu" :: Nil))
println( "site6 反转前 : " + site6 )
println( "site6 反转后 : " + site6.reverse )
//Set集合
val site7 = Set("Runoob", "Google", "Baidu")
val nums6: Set[Int] = Set()
println( "第一网站是 : " + site7.head )
println( "最后一个网站是 : " + site7.tail )
println( "查看列表 site 是否为空 : " + site7.isEmpty )
println( "查看 nums 是否为空 : " + nums6.isEmpty )
// ++ 作为运算符使用--连接
var site8 = site7 ++ nums6
println( "site1 ++ site2 : " + site8 )
val site10 = Set("Faceboook", "Taobao")
// ++ 作为方法使用
site8 = site7.++(site10)
println( "site1.++(site2) : " + site8 )
val num = Set(5,6,9,20,30,45) // 查找集合中最大与最小元素
println( "Set(5,6,9,20,30,45) 集合中的最小元素是 : " + num.min )
println( "Set(5,6,9,20,30,45) 集合中的最大元素是 : " + num.max )
val num1 = Set(5,6,9,20,30,45)
val num3 = Set(50,60,9,20,35,55) // 交集
println( "num1.&(num2) : " + num1.&(num3) )
println( "num1.intersect(num2) : " + num1.intersect(num3) )
//Map映射
var A:Map[Char,Int] = Map()
A += ('I' -> 1)
A += ('J' -> 5)
A += ('K' -> 10)
A += ('L' -> 100)
println( "colors 中的键为 : " + A.keys )
println( "colors 中的值为 : " + A.values )
println( "检测 colors 是否为空 : " + A.isEmpty )
//连接
val colors1 = Map("red" -> "#FF0000",
"azure" -> "#F0FFFF",
"peru" -> "#CD853F")
val colors2 = Map("blue" -> "#0033FF",
"yellow" -> "#FFFF00",
"red" -> "#FF0000") // ++ 作为运算符
var colors = colors1 ++ colors2
println( "colors1 ++ colors2 : " + colors ) // ++ 作为方法
colors = colors1.++(colors2)
println( "colors1.++(colors2)) : " + colors )
//foreach循环
val sites = Map("runoob" -> "http://www.runoob.com",
"baidu" -> "http://www.baidu.com",
"taobao" -> "http://www.taobao.com") sites.keys.foreach{ i =>
print( "Key = " + i )
println(" Value = " + sites(i) )}
if( sites.contains( "runoob" )){
println("runoob 键存在,对应的值为 :" + sites("runoob"))
}else{
println("runoob 键不存在")
}
if( sites.contains( "baidu" )){
println("baidu 键存在,对应的值为 :" + sites("baidu"))
}else{
println("baidu 键不存在")
}
if( sites.contains( "google" )){
println("google 键存在,对应的值为 :" + sites("google"))
}else{
println("google 键不存在")
}
//Iterator(迭代器)
/**
* Scala Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法。
迭代器 it 的两个基本操作是 next 和 hasNext。
调用 it.next() 会返回迭代器的下一个元素,并且更新迭代器的状态。
调用 it.hasNext() 用于检测集合中是否还有元素。
让迭代器 it 逐个返回所有元素最简单的方法是使用 while 循环:
*/
val it = Iterator("Baidu", "Google", "Runoob", "Taobao")
while (it.hasNext){
println(it.next())
}
val ita = Iterator(20,40,2,50,69, 90)
val itb = Iterator(20,40,2,50,69, 90)
println("最大元素是:" + ita.max )
println("最小元素是:" + itb.min )
println("ita.size 的值: " + ita.size )
println("itb.length 的值: " + itb.length )
}
}
Spark记录-Scala数组/List/Map/Set的更多相关文章
- Spark记录-Scala数组
Scala提供了一种数据结构叫作数组,数组是一种存储了相同类型元素的固定大小顺序集合.数组用于存储数据集合,但将数组视为相同类型变量的集合通常更为有用. 可以声明一个数组变量,例如:numbers,使 ...
- Spark记录-Scala集合
Scala列表 Scala列表与数组非常相似,列表的所有元素都具有相同的类型,但有两个重要的区别. 首先,列表是不可变的,列表的元素不能通过赋值来更改. 其次,列表表示一个链表,而数组是平的. 具有类 ...
- Spark记录-Scala程序例子(函数/List/match/option/泛型/隐式转换)
object func { def main(args:Array[String]):Unit={ //函数赋值给变量时, 必须在函数后面加上空格和下划线. def sayHello(name: St ...
- Spark记录-scala快速入门
1.hello world程序 object HelloWorld { def main(args: Array[String]) { println("Hello,World!" ...
- Spark记录-Scala记录(基础程序例子)
import scala.util.control._ object learnning { def main(args:Array[String]):Unit={ val n:Int=10 prin ...
- Spark记录-Scala语法基础
参考:http://docs.scala-lang.org/cheatsheets/index.html.http://docs.scala-lang.org/.http://www.scala-la ...
- Spark记录-Scala字符串
Scala字符串 在Scala中的字符串和Java中的一样,字符串是一个不可变的对象,也就是一个不能修改的对象.可以修改的对象,如数组,称为可变对象.字符串是非常有用的对象,在本节的最后部分,我们将介 ...
- Spark记录-Scala shell命令
1.scala shell命令 scala> :help All commands can be abbreviated, e.g., :he instead of :help. :edit & ...
- Spark记录-Scala函数
Scala函数 Scala有函数和方法. Scala方法是一个具有名称和签名的类的一部分. Scala中的函数是一个可以分配给变量的完整对象. 函数定义可以出现在源文件中的任何位置. 不带参数的函数 ...
随机推荐
- effective c++ 笔记 (9-12)
//---------------------------15/03/29---------------------------- //#9 绝不在构造和析构过程中调头virtual函数 { / ...
- python + selenium webdriver 自动化测试 之 环境异常处理 (持续更新)
1.webdriver版本与浏览器版本不匹配,在执行的时候会抛出如下错误提示 selenium.common.exceptions.WebDriverException: Message: unkno ...
- 详细聊聊k8s deployment的滚动更新(一)
一.知识准备 ● 本文详细探索deployment在滚动更新时候的行为 二.环境准备 组件 版本 OS Ubuntu 18.04.1 LTS docker 18.06.0-ce 三.准备镜像 首先准备 ...
- PAT甲题题解-1022. Digital Library (30)-map映射+vector
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789235.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- SCRUM 12.14
由于最近的课业较多,大家平时有些力不从心,对于工作完成得有限. 最近课业压力小了一些,我们决定从今天起,投入精力. 以下为我们的任务分配情况: 人员 任务 高雅智 清除缓存 彭林江 统计活跃用户数量 ...
- 20135220谈愈敏Linux Book_5
第五章 系统调用 内核提供了用户进程与内核进行交互的一组接口. 应用程序发出请求->内核负责满足 目的:保证系统稳定可靠 5.1 与内核通信 系统调用在用户空间进程和硬件设备之间添加了一个中间层 ...
- spring boot 添加整合ssl使得http变成https方法
1. https出现的背景:(1)都知道http传输协议是裸漏的,明文传输的,极易被黑客拦截,因此,(2)人们想出的使用加密,也就是 对称加密 例如aes,不过这个由于因为对称加密需要每个客户端和服务 ...
- Alpha 冲刺九
团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 完善各自部分 项目描 ...
- Unbuntu18.04通过apt源方式安装mysql5.7.22
Ubuntu18.04在今年4月底发布了,喜欢尝鲜的小伙伴肯定是第一时间就更新了系统版本,那么在Ubuntu18.04中怎么安装msyql5.7(mysql8.0因为新出,再观望一段时间)呢? * 带 ...
- mysql 清空数据
清空数据有2 个命令 -- 清空全部数据,不写日志,不可恢复,速度极快 truncate table 表名; -- 清空全部数据,写日志,数据可恢复,速度慢 delete from 表名 业务需求:清 ...