creating and adding elements to a 1.List list
1) One of the most common ways to create a list.
scala> val list = 1:: 2:: 3:: nillist:list[int] = List (1, 2, 3)
2) One of the most common ways to create a list.
scala> val list = list (list:list[int) = List (1, 2, 3)
3) Set of mixed-type composition.
scala> val list = list (1,2.0,33d,4000l) list:list[double] = List (1.0, 2.0, 33.0, 4000.0)
4) Set mixed type composition, can have their own control. The collection of the following example preserves the type of the original collection.
scala> val list = List[number] (1,2.0,33d,4000l) list:list[number] = List (1, 2.0, 33.0, 4000)
5) range creates and fills the collection.
scala> val list = List.range (1,10) list:list[int] = List (1, 2, 3, 4, 5, 6, 7, 8, 9)
6) Fill creates and fills the collection.
scala> val list = List.fill (3) ("Hello") list:list[string] = list (hello, hello, hello)
7) tabulate create and populate the collection.
scala> val list = List.tabulate (5) (i = i * i) list:list[int] = List (0, 1, 4, 9, 16)
8) Convert the collection to the form of a list.
scala> val list = Collection.mutable.ListBuffer (1, 2, 3). tolistlist:list[int] = List
9) Convert the collection to the form of a list.
Scala> "Hello". Tolistres41:list[char] = List (h, E, L, L, O)
10) Create a mutable list by using Listbuffer and converting Listbuffer to list.
scala> import Scala.collection.mutable.ListBufferimport scala.collection.mutable.listbufferscala> var fruits = New Listbuffer[string] () fruits:scala.collection.mutable.listbuffer[string] = Listbuffer () scala> fruits + = "apple" Res42:scala.collection.mutable.listbuffer[string] = Listbuffer (apple) scala> fruits + = "Orange" RES43: Scala.collection.mutable.listbuffer[string] = Listbuffer (apple, orange) scala> fruits + = ("banana", "grape", "pear") Res44:scala.collection.mutable.listbuffer[string] = Listbuffer (apple, orange, banana, grape, pear) scala> val Fruitslist = fruits.tolistfruitslist:list[string] = List (apple, orange, banana, grape, pear)
One) use the:: method to add elements before the list.
scala> var list = list (2) List:list[int] = List (2) scala> list = 1:: listlist:list[int] = List (1, 2) scala> list = 9:: listlist:list[int] = List (9, 1, 2)
2. Removing elements from List (Listbuffer)
1) The list is immutable and cannot be removed from the element, but it can filter out unwanted elements and assign the result to a new variable.
scala> val list = list (4,5,2,1,3) list:list[int] = List (4, 5, 2, 1, 3) scala> val NewList = List.filter (_ > 2) newl Ist:list[int] = List (4, 5, 3)
2) The way that repeated operations like this can be assigned to a variable is avoidable by declaring the variable Var, and then returning the result of each operation to the variable.
scala> var list = list (5,2,3,4,1) list:list[int] = List (5, 2, 3, 4, 1) scala> list = List.filter (_ > 2) list:list[ INT] = List (5, 3, 4)
3) If the list changes frequently, using Listbuffer is a good choice. Listbuffer is mutable, so you can use all methods of a mutable sequence to remove an element from it.
Import scala.collection.mutable.listbufferscala> val x = Listbuffer (1,2,3,4,5,6,7,8,9) x: Scala.collection.mutable.listbuffer[int] = Listbuffer (1, 2, 3, 4, 5, 6, 7, 8, 9)//You can delete one element at a time by value:scala> x-= 5res45:x.ty PE = listbuffer (1, 2, 3, 4, 6, 7, 8, 9)//can delete two or more elements at a time:scala> x-= (2,3,4) Res46:x.type = Listbuffer (1, 6, 7, 8, 9)// You can delete elements by location:scala> x.remove (0) Res47:int = 1scala> Xres48:scala.collection.mutable.listbuffer[int] = ListBuffer (6 , 7, 8, 9)//remove can delete a specified number of elements from a fixed starting position:scala> X.remove (1,3) scala> Xres50:scala.collection.mutable.listbuffer[int] = Listbuffer (6)//The element can be removed from the specified collection using the--= method. scala> val x = Listbuffer (1,2,3,4,5,6,7,8,9) X:scala.collection.mutable.listbuffer[int] = ListBuffer (1, 2, 3, 4, 5, 6, 7, 8,9) scala> x--= Seq (all in a) Res51:x.type = Listbuffer (4, 5, 6, 7, 8, 9)
3. Merging or connecting the list
1) Merge two lists using the + + method
scala> val A = list (a:list[int) = List (1, 2, 3) scala> val b = List (4,5,6) b:list[int] = List (4, 5, 6) Scala> ; Val C = A + Bc:list[int] = List (1, 2, 3, 4, 5, 6)
2) Use::: Merge two lists
scala> val A = list (a:list[int) = List (1, 2, 3) scala> val b = List (4,5,6) b:list[int] = List (4, 5, 6) Scala> ; Val C = A::: b
3) Merge two lists with Concat
scala> val A = list (a:list[int) = List (1, 2, 3) scala> val b = List (4,5,6) b:list[int] = List (4, 5, 6) Scala> ; Val C = List.concat (A, b) c:list[int] = List (1, 2, 3, 4, 5, 6)