惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - cindy_zl

SAP SD销售与分销模块 Agent认知框架ReAct&Plan RAG系统优化 RAG检索优化 RAG架构 SAA-C02-架构师助理级-英文版-Q1 scala-数组 navicat mysql创建存储过程报错 安装完kali出现中文乱码 Spark SQL是处理结构化的数据 SPARK SQL ERROR: Detected cartesian product for INNER join between logical plans报错解决方法 scala的lazy关键字 scala集合三大类(seq序列,set集,map映射)——map映射 scala集合三大类(seq序列,set集,map映射)——set集合 scala集合三大类(seq序列,set集,map映射)——list序列 scala元组及拉链操作 scala的map映射 scala方法和涵数的声明以及方法转换成涵数 scala的基础数据类型&if条件表达式&for循环
scala数组
cindy_zl · 2019-09-17 · via 博客园 - cindy_zl

scala数组:分为定长数组和变长数组

scala> val arr1 = new Array[Int](8)             //只定义8个是整型类型的定长数组,没有赋值,每个数组里面的值是0

arr1: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> val arr1 = new Array[String](8)         //只定义8个是字符串类型的定长数组,没有赋值,每个值显示是为null

arr1: Array[String] = Array(null, null, null, null, null, null, null, null)

scala> println(arr1)                                  //直接打印数组是一个引用,并没有打印出数组里面的内容。

[Ljava.lang.String;@4973f7dd

scala> println(arr1.toBuffer)                     //使用toBuffer方法将数组里面的内容转换成数组缓冲里面,将其打印出来。

ArrayBuffer(null, null, null, null, null, null, null, null)

下面是直接赋值方式定义一个数组:

scala> val arr2 = Array("java","scala","python")     //没有用new是调用的一个静态方法,并给数组里面每个元素赋值

arr2: Array[String] = Array(java, scala, python)

scala> val arr2 = new Array("java","scala","python") //使用一个new关键字直接赋值定义是错误的写法

                                   ^

       error: too many arguments (3) for constructor Array: (_length: Int)Array[T]

 数组的调用方法数组名后面小括号里面跟下标(下标从0开始)

scala> println(arr2(0))

java

变长数组:scala里面默认的是一个定长数组,我们要用变长数组需要导入一个变长数组所依赖的包:

scala> import scala.collection.mutable.ArrayBuffer

import scala.collection.mutable.ArrayBuffer

scala> val arr3 = ArrayBuffer[Int]()                         //需要指定一个Int类型的一个范型

arr3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> arr3 += 1                   //向变长数组里面添加一个数据

res17: arr3.type = ArrayBuffer(1)

scala> arr3 += (2,3,4)           //向变长数组里面添加多个数据

            ^

       warning: method += in trait Growable is deprecated (since 2.13.0): Use `++=` (addAll) instead of varargs `+=`

res18: arr3.type = ArrayBuffer(1, 2, 3, 4)   //较高版本已经不太友好的使用+=来添加多个元素了,提示使用++=方法向变长数组里面添加数据

scala> arr3 ++= Array(5, 6)                    //向变长数组里面添加一个定长数组里面的内容

res20: arr3.type = ArrayBuffer(1, 2, 3, 4, 5, 6)

scala> arr3 ++= ArrayBuffer(7,8)             //向变长数组里面添加一个变长数组里面的内容

res21: arr3.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

变长数组的添加与删除 

scala> arr3.insert(0, -1)                      //第 0 个位置插入 -1 

scala> arr3.remove(2,2)                     //移除从第二个位置开始两个元素

scala> println(arr3.toBuffer)

ArrayBuffer(-1, 1, 4, 5, 6, 7, 8)

for循环取数组里面的数:

scala> for(i<-arr3) println(i)

-1

1

4

5

6

7

8

scala> for(i<- 0 until arr3.length) println(arr3(i))

-1

1

4

5

6

7

8

scala> for(i<- (0 until arr3.length).reverse) println(arr3(i))

8

7

6

5

4

1

-1

scala> val res = for(i<-arr3) yield i*10     //yield是把数组里面的每一个值都取出来,并且把每个值都放到一个集合里面的这一个作用。并且会产生一个新的数组res

res: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-10, 10, 40, 50, 60, 70, 80)

scala> res

res34: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-10, 10, 40, 50, 60, 70, 80)

scala> arr3.sum

res35: Int = 30

scala> arr3.max

res36: Int = 8

scala> arr3.sorted

res37: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 1, 4, 5, 6, 7, 8)

scala> arr3.min

res38: Int = -1