当前位置:网站首页>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.
边栏推荐
- memcached基础11
- Oracle/PLSQL: Lpad Function
- Learn Tai Chi maker mqtt (IX) esp8266 subscribe to and publish mqtt messages at the same time
- Oracle/PLSQL: Rtrim Function
- P5.js death planet
- Look! In June, 2022, the programming language ranking list was released! The first place is awesome
- Oracle/PLSQL: Upper Function
- mmdetection ValueError: need at least one array to concatenate解决方案
- Consumers pursue the iPhone because its cost performance exceeds that of domestic mobile phones
- 【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
猜你喜欢

Svg drag dress Kitty Cat

canvas粒子篇之鼠标跟随js特效

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

Learn Tai Chi Maker - mqtt Chapter 2 (3) reserved messages

Constraintlayout Development Guide

Flink学习4:flink技术栈

three. JS domino JS special effect

Hot discussion: what are you doing for a meaningless job with a monthly salary of 18000?

Flink learning 3: data processing mode (stream batch)

Cvpr2022 | pointdistiller: structured knowledge distillation for efficient and compact 3D detection
随机推荐
参数估计——《概率论及其数理统计》第七章学习报告(点估计)
Oracle/PLSQL: VSize Function
paddlepaddle 19 动态修改模型的最后一层
Oracle/PLSQL: NumToYMInterval Function
Fork (), exec (), waitpid (), $? > > in Perl 8 combination
mmdetection ValueError: need at least one array to concatenate解决方案
学习太极创客 — MQTT(九)ESP8266 同时订阅和发布 MQTT 消息
【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
Oracle/PLSQL: Rpad Function
Flink學習2:應用場景
Mmdetection uses yolox to train its own coco data set
Oracle/PLSQL: Lower Function
I earned 3W yuan a month from my sideline: the industry you despise really makes money!
Flink学习1:简介
Is the division of each capability domain of Dama, dcmm and other data management frameworks reasonable? Is there internal logic?
Flink学习4:flink技术栈
Oracle/PLSQL: To_Clob Function
svg拖拽装扮Kitty猫
What if asreml-r does not converge in operation?
h5液体动画js特效代码