Scala’s sets are also divided into immutable sets and mutable sets. The Set includes the sequence Seq, the Set Set and the Map, which are also divided into immutable and mutable.

Immutable List

Define the List, including apply, :: Nil, and ::: to define the List. Nil is an empty collection, and :: is a new list made from the head element and tail list, and this is evaluated from the end, so the following is evaluated first with respect to 3::Nil, then 2::(3 ::Nil), and finally 1:: ::Nil). If head is a List, the first element is still a List, so to form a new List with :::, see list3, list4 below.

List1: List[Int] = List(1, 2, 3) list2: List[Int] = List(1, 2, 3) list2: List[Int] = 1: : 2: : 3: : Nil val list3: List[Any] = list1 :: list2 val list4: List[Int] = list1 ::: list2 println(list1) // List(1, 2, 3) println(list2) // List(1, 2, 3) println(list3) // List(List(1, 2, 3), 1, 2, 3) println(list4) // List(1, 2, 3, 1, 2, 3)a

Read the List:

Println (list1.head) println(list1.head) println(list1.head) println(list1.head) println(list1.head) println(list1.tail) Println (list1.init) println(list1.init) println(list1.init) println(list1.init) println(list1.init) println(list1.init) // Println (list1.take(2)) // List(1) println(list1.take(2)) // List(1) println(list1.take(2)) Println (list1.mkString("-"))) // 1-2-3

New Elements:

Println (list5_1) // List(100, 1, 2, 3) val list5_2 = 100 +:(100) println(list5_1) list1 println(list5_2) // List(100, 1, 2, 3) val list5_3 = 100 :: list1 println(list5_3) // List(100, 1, 2, 3) val list5_4 = list1.::(100) println(list5_4) // List(100, 1, 2, Println (list6_1) // List(1, 2, 3, 1) 4) val list6_2 = list1 :+ 4 println(list6_2) // List(1, 2, 3, 4)

The same array I’m iterating over, which I’m not going to demonstrate here.

Variable ListBuffer

Define the ListBuffer

ListBuffer[Int] = listBuffer () val list2: listBuffer [Int] = listBuffer () val list2: ListBuffer[Int] = ListBuffer(1, 2, 3) println(list1) // ListBuffer() println(list2) // ListBuffer(1, 2, 3)

Read the listBuffer, as above. Modify the specified element:

list2(0) = 11
println(list2) // ListBuffer(11, 2, 3)
list2.update(0, 1)
println(list2) // ListBuffer(1, 2, 3)

New Elements:

Println (1, 2, 3, 4) listBuffer (1, 2, 3, 4) println(1, 2, 3, 4) listBuffer (1, 2, 3, 4) println(2, 4) 5) list2.append(6) println(list2) // ListBuffer(1, 2, 3, 4, 5, Println (list2) // listBuffer (100, 1, 2, 3, 4, 5, 6) // listBuffer (100, 1, 2, 3, 4, 5, 6). 101) println(list2) // ListBuffer(100, 101, 1, 2, 3, 4, 5, 6)

Delete elements. It’s like an array

list2.remove(0)
println(list2) // ListBuffer(101, 1, 2, 3, 4, 5, 6)
list2.remove(1, 3)
println(list2) // ListBuffer(101, 4, 5, 6)
list2 -= 4
println(list2) // ListBuffer(101, 5, 6)

The same array I’m iterating over, which I’m not going to demonstrate here.