当前位置:网站首页>Getting started with Scala_ Immutable list and variable list
Getting started with Scala_ Immutable list and variable list
2022-06-27 02:22:00 【Big data interview classic】
list
The list is scala The most important 、 It's also the most common data structure .List It has the following properties :
- You can save duplicate values
- In order
stay scala in , There are also two kinds of lists , One is immutable lists 、 The other is the variable list
Definition
Immutable lists are elements of lists 、 The length is immutable .
grammar
Use List( Elements 1, Elements 2, Elements 3, ...) To create an immutable list , Grammar format :
val/var Variable name = List( Elements 1, Elements 2, Elements 3...)
- 1.
Use Nil Create an immutable empty list
val/var Variable name = Nil
- 1.
Use :: Method to create an immutable list
val/var Variable name = Elements 1 :: Elements 2 :: Nil
- 1.
[!TIP]
Use **:: Create a list by splicing , You have to add one at the end Nil**
Example 1
Create an immutable list , Store the following elements (1,2,3,4)
Reference code
scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)
- 1.
- 2.
Example 2
Use Nil Create an immutable empty list
Reference code
scala> val a = Nil
a: scala.collection.immutable.Nil.type = List()
- 1.
- 2.
Example 3
Use :: Method to create a list , contain -2、-1 Two elements
Reference code
scala> val a = -2 :: -1 :: Nil
a: List[Int] = List(-2, -1)
- 1.
- 2.
Variable list
Variable lists are elements of lists 、 The length is variable .
To use variable lists , First import import scala.collection.mutable.ListBuffer
[!NOTE]
- Variable sets are all in
mutable In bag- Immutable sets are all in
immutable In bag ( The default import )
Definition
Use ListBuffer[ Element type ]() Create an empty mutable list , Grammatical structure :
val/var Variable name = ListBuffer[Int]()
- 1.
Use ListBuffer( Elements 1, Elements 2, Elements 3…) Create variable lists , Grammatical structure
Example 1
Create an empty variable list
Reference code
scala> val a = ListBuffer[Int]()
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
- 1.
- 2.
Example 2
Create a mutable list , Contains the following elements :1,2,3,4
Reference code
scala> val a = ListBuffer(1,2,3,4)
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
- 1.
- 2.
Variable list operations
- Get elements ( Use parentheses to access
( Index value )) - Additive elements (
+=) - Add a list (
++=) - Change the element (
Use parentheses to get elements , And then assign a value ) - Remove elements (
-=) - Convert to List(
toList) - Convert to Array(
toArray)
Example
- Define a variable list that contains the following elements :1,2,3
- Get first element
- Add a new element :4
- Add a list , The list contains the following elements :5,6,7
- Remove elements 7
- Convert variable list to immutable list
- Convert variable list to array
Reference code
// Import immutable list
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
// Create immutable lists
scala> val a = ListBuffer(1,2,3)
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
// Get first element
scala> a(0)
res19: Int = 1
// Add an element
scala> a += 4
res20: a.type = ListBuffer(1, 2, 3, 4)
// Add a list
scala> a ++= List(5,6,7)
res21: a.type = ListBuffer(1, 2, 3, 4, 5, 6, 7)
// Remove elements
scala> a -= 7
res22: a.type = ListBuffer(1, 2, 3, 4, 5, 6)
// Convert to immutable list
scala> a.toList
res23: List[Int] = List(1, 2, 3, 4, 5, 6)
// Convert to array
scala> a.toArray
res24: Array[Int] = Array(1, 2, 3, 4, 5, 6)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
边栏推荐
- Oracle/PLSQL: NumToDSInterval Function
- Introduction to stm32
- Mmdetection uses yolox to train its own coco data set
- "All majors are persuading them to quit." is it actually the most friendly to college students?
- Flink learning 3: data processing mode (stream batch)
- Microsoft365 developer request
- JWT certification process and use cases
- Memcached foundations 12
- lottie.js创意开关按钮动物头像
- 谷歌开始卷自己,AI架构Pathways加持,推出200亿生成模型
猜你喜欢

WiFi-IoT 鸿蒙开发套件样例开发

平均风向风速计算(单位矢量法)

Consumers pursue the iPhone because its cost performance exceeds that of domestic mobile phones

解决cherry pick提交报错问题

学习太极创客 — MQTT(九)ESP8266 同时订阅和发布 MQTT 消息

Look! In June, 2022, the programming language ranking list was released! The first place is awesome

Learning Tai Chi Maker - mqtt Chapter 2 (II) esp8266 QoS application

Enterprise digital transformation: informatization and digitalization

P5.js death planet

Flink learning 3: data processing mode (stream batch)
随机推荐
Look! In June, 2022, the programming language ranking list was released! The first place is awesome
ConstraintLayout(约束布局)开发指南
Flink learning 5: how it works
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
pytorch_grad_cam——pytorch下的模型特征(Class Activation Mapping, CAM)可视化库
lottie.js创意开关按钮动物头像
Paddlepaddle 19 dynamically modify the last layer of the model
Hot discussion: what are you doing for a meaningless job with a monthly salary of 18000?
Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
Oracle/PLSQL: Substr Function
How does the C # TCP server limit the number of connections to the same IP?
Memcached basics 15
Oracle/PLSQL: Lower Function
Introduction to stm32
Flink学习5:工作原理
Oracle/PLSQL: NumToYMInterval Function
Oracle/PLSQL: NumToDSInterval Function
Oracle/PLSQL: Length Function
消费者追捧iPhone,在于它的性价比超越国产手机
canvas粒子篇之鼠标跟随js特效