当前位置:网站首页>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.
边栏推荐
猜你喜欢

Press key to control LED status reversal

Summer planning for the long river

Cvpr2022 | pointdistiller: structured knowledge distillation for efficient and compact 3D detection

Learn Tai Chi Maker - mqtt (VIII) esp8266 subscribe to mqtt topic

Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work

docker部署redis集群

参数估计——《概率论及其数理统计》第七章学习报告(点估计)

Constraintlayout Development Guide

Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched

我靠副业一个月挣了3W块:你看不起的行业,真的很挣钱!
随机推荐
学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息
paddlepaddle 20 指数移动平均(ExponentialMovingAverage,EMA)的实现与使用(支持静态图与动态图)
Memcached basics 15
memcached基础10
Flink Learning 2: Application Scenarios
Is the division of each capability domain of Dama, dcmm and other data management frameworks reasonable? Is there internal logic?
正则表达式:语法
Oracle/PLSQL: Cast Function
Memcached basics 14
解决cherry pick提交报错问题
paddlepaddle 21 基于dropout实现用4行代码dropblock
P5.js death planet
Mmdetection uses yolox to train its own coco data set
Memcached foundation 9
Svg drag dress Kitty Cat
TechSmith Camtasia最新2022版详细功能讲解下载
Uninstallation of Dameng database
Oracle/PLSQL: Upper Function
Flink learning 1: Introduction
Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work