当前位置:网站首页>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: VSize Function
- Oracle/PLSQL: Lower Function
- C language -- Design of employee information management system
- Oracle/PLSQL: CharToRowid Function
- H5 liquid animation JS special effect code
- 平均风向风速计算(单位矢量法)
- Oracle/PLSQL: Trim Function
- C # check whether the date is in the correct format
- Microsoft365开发人员申请
- Learn Tai Chi Maker - mqtt (VIII) esp8266 subscribe to mqtt topic
猜你喜欢

Press key to control LED status reversal

Flink learning 2: application scenarios
![[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix](/img/e4/7bae2a109dcf5e2a8f032e73b89479.png)
[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix

Docker deploy redis cluster

学习太极创客 — MQTT(八)ESP8266订阅MQTT主题

DAMA、DCMM等数据管理框架各个能力域的划分是否合理?有内在逻辑吗?

学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息

lottie.js创意开关按钮动物头像

学习太极创客 — MQTT 第二章(二)ESP8266 QoS 应用

Flink learning 1: Introduction
随机推荐
Learning Tai Chi Maker - mqtt (VII) advanced mqtt theme
Sample development of WiFi IOT Hongmeng development kit
Flink学习2:应用场景
Paddlepaddle 19 dynamically modify the last layer of the model
WiFi-IoT 鸿蒙开发套件样例开发
学习太极创客 — MQTT 第二章(三)保留消息
ConstraintLayout(约束布局)开发指南
Microsoft365开发人员申请
剑指Offer || :栈与队列(简单)
p5.js死亡星球
d的appendTo包装
Oracle/PLSQL: Ltrim Function
P5.js death planet
Oracle/PLSQL: Ltrim Function
paddlepaddle 19 动态修改模型的最后一层
paddlepaddle 21 基于dropout实现用4行代码dropblock
three.js多米诺骨牌js特效
Uninstallation of Dameng database
Oracle/PLSQL: VSize Function
Oracle/PLSQL: NumToDSInterval Function