Article Directory
- concept
- Immutable list
- Variable list
- A list of common operations
concept
Scala list is the most important, is the most commonly used data structures. List has the following properties:
- You can save duplicate value
- There the order
In the scala, there are two lists, one is immutable list, another list of variable
Immutable list
concept
An immutable list is a list of elements, the length is immutable.
definition
grammar
//使用List(元素1, 元素2, 元素3, ...)来创建一个不可变列表,语法格式
val/var 变量名 = List(元素1, 元素2, 元素3...)
//使用Nil创建一个不可变的空列表
//使用 :: 拼接方式来创建列表,必须在最后添加一个 Nil
val/var 变量名 = 元素1 :: 元素2 :: Nil
The sample code
//创建一个不可变列表,存放以下几个元素(1,2,3,4)
val a = List(1,2,3,4)
//使用Nil创建一个不可变的空列表
val a = Nil
//使用 :: 方法创建列表,包含-2、-1两个元素
//完整写法
val d: List[Int] = -2 :: -1 :: Nil
//省略写法
val a = -2 :: -1 :: Nil
Variable list
concept
The variable list is a list of elements, the length is variable.
To use the variable list, first guide package
import scala.collection.mutable.ListBuffer
- Variable set in
mutable
the package- Immutable set in
immutable
the package (default import)
definition
Syntax
//使用ListBuffer[元素类型]()创建空的可变列表,语法结构
val/var 变量名 = ListBuffer[Int]()
//使用ListBuffer(元素1, 元素2, 元素3...)创建可变列表,语法结构
val/var 变量名 = ListBuffer(元素1,元素2,元素3...)
The sample code
//导包
import scala.collection.mutable.ListBuffer
//创建空的整形可变列表
val a = ListBuffer[Int]()
//创建一个可变列表,包含以下元素:1,2,3,4
val a = ListBuffer(1,2,3,4)
Operation of the variable list
- Gets the element (using brackets to access
(索引值)
)- Add elements
+=
( )- Append a list
++=
( )- Change Element
使用括号获取元素,然后进行赋值
( )- Remove elements
-=
( )- Convert List
toList
( )- Convert Array
toArray
( )
The sample code
//导包
import scala.collection.mutable.ListBuffer
//1. 定义一个可变列表包含以下元素:1,2,3
val a: ListBuffer[Int] = ListBuffer(1,2,3)
//2. 获取第一个元素
//方式一
a(0)
//方式二
a.head
//3. 添加一个新的元素:4
a += 4
//4. 追加一个列表,该列表包含以下元素:5,6,7
a ++= List(5,6,7)
//5. 删除元素7
a -= 7
//6. 将可变列表转换为不可变列表
a.toList
//7. 将可变列表转换为数组
a.toArray
A list of common operations
- Determine whether the list is empty (
isEmpty
)- Splicing two lists
++
( )- Get a list of the first element (
head
) and the remaining portion (tail
)- Reverse List
reverse
( )- Get the prefix of
take
( ) to get the suffixdrop
( )- Flattening
flaten
( )- Zip (
zip
) and pullunzip
( )- Conversion string
toString
( )- Generating a string
mkString
( )- And set
union
( )- Intersection
intersect
( )- Difference set
diff
( )
Determine whether the list is empty
The sample code
//定义一个列表,包含以下元素:1,2,3,4
val a = List(1,2,3,4)
//判空
a.isEmpty
Splicing two lists
The sample code
//定义列表一
val a = List(1,2,3)
//定义列表二
val b = List(4,5,6)
//拼接
a ++ b
Get a list of the first element and the remaining part
The sample code
//定义一个列表
val a = List(1,2,3)
//获取首个元素
a.head
//获取剩余部分
a.tail
Reverse list
The sample code
//定一个列表,包含以下元素:1,2,3
val a = List(1,2,3)
//反转
a.reverse
Get a list of prefixes and suffixes
The sample code
//定一个列表,包含以下元素:1,2,3,4,5
val a = List(1,2,3,4,5)
//使用take方法获取前缀(前三个元素):1,2, 3
a.take(3)
//使用drop方法获取后缀(除前三个以外的元素):4,5
a.drop(3)
Flattening (applanation)
Shows a flat list of all the elements in the list into a list.
The sample code
//有一个列表,列表中又包含三个列表,分别为:List(1,2)、List(3)、List(4,5)
val a = List(List(1,2), List(3), List(4,5))
//使用flatten将这个列表转换为List(1,2,3,4,5)
a.flatten
Convert a string
toString method returns all elements List
The sample code
//定义一个列表,包含以下元素:1,2,3,4
val a = List(1,2,3,4)
//使用toString输出该列表的元素
println(a.toString)
Generating a string
mkString method, the elements can be spliced together with a delimiter. The default is no delimiter
The sample code
//定义一个列表,包含以下元素1,2,3,4
val a = List(1,2,3,4)
//使用mkString,用冒号将元素都拼接起来
a.mkString(":")
Union
It represents a union of two sets, and to take the list, not weight
The sample code
// 定义第一个列表,包含以下元素:1,2,3,4
val a = List(1,2,3,4)
// 定义第二个列表,包含以下元素:3,4,5,6
val b = List(3,4,5,6)
// 使用union操作,获取这两个列表的并集
a.union(b)
// 使用distinct操作,去除重复的元素
a.union(b).distinct
Intersection
intersect represents the intersection of the two lists take
The sample code
// 定义第一个列表,包含以下元素:1,2,3,4
val a1 = List(1,2,3,4)
// 定义第二个列表,包含以下元素:3,4,5,6
val a2 = List(3,4,5,6)
// 使用intersect操作,获取这两个列表的交集
a1.intersect(a2)
Difference set
diff expressed taking the difference of two sets list, for example: a1.diff (a2), a1 represents the element acquired in the absence of the a2
The sample code
//定义第一个列表,包含以下元素:1,2,3,4
val a1 = List(1,2,3,4)
//定义第二个列表,包含以下元素:3,4,5,6
val a2 = List(3,4,5,6)
//使用diff获取这两个列表的差集
a1.diff(a2)