1. Array
1.1 general
Array is a container used to store multiple elements of the same type. Each element has a number (also known as subscript, subscript and index), and the number starts from 0. In Scala, there are two kinds of arrays, one is a fixed length array and the other is a variable length array
1.2 fixed length array
1.2.1 features
1. The length of the array cannot be changed. 2. The contents of the array are mutable
1.2.2 syntax
val/var Variable name = new Array[Element type](Array length) val/var Variable name = Array(Element 1, Element 2, Element 3...)
be careful:
- In scala, array generics are specified with []
- Use the array name (index) to get the elements in the array
- Array elements have default values, Int:0, Double:0.0, String: null
- Get the length of the array through the array name. Length or array name. size
1.2.3 example
- Define an integer array with a length of 10, set the first element to 11, and print the first element
- Define an array containing "java", "scala" and "python", and print the length of the array
//Case: demonstration of fixed length array object ClassDemo01 { def main(args: Array[String]): Unit = { //1. Define an integer array with a length of 10, set the first element to 11, and print the first element val arr1 = new Array[Int](10) arr1(0) = 11 println(arr1(0)) //Print the first element of the array println("-" * 15) //Split line //2. Define an array containing "Java", "Scala" and "Python" and print the array length val arr2 = Array("java", "scala", "python") println(arr2.length) //Print the length of the array } }
1.3 variable length array
1.3.1 features
The length and content of the array are variable. You can add and delete elements to the array
1.3.2 syntax
To create a variable length array, you need to import the ArrayBuffer class first
import scala.collection.mutable.ArrayBuffer
Definition format 1: create an empty ArrayBuffer variable length array
val/var Variable name = ArrayBuffer[Element type]()
Definition format 2: create an ArrayBuffer variable length array with initial elements
val/var Variable name = ArrayBuffer(Element 1, element 2, element 3....)
1.3.3 example 1: defining variable length arrays
- Defines an integer variable length array with length 0
- Define a variable length array containing "hadoop", "storm" and "spark"
- Print results. Reference code
Reference code
//1. Guide package import scala.collection.mutable.ArrayBuffer //Case: demonstrating variable length arrays object ClassDemo02 { def main(args: Array[String]): Unit = { //2. Define an integer variable length array with length 0 val arr1 = new ArrayBuffer[Int]() println("arr1:" + arr1) //3. Define a variable length array containing "Hadoop", "storm" and "spark" val arr2 = ArrayBuffer("hadoop", "storm", "spark") println("arr2:" + arr2) } }
1.3.4 example 2: adding, deleting and modifying elements
For variable length arrays in Scala, you can modify the contents of the array in the following ways
format
use += Add a single element use -= Delete a single element use ++= Append an array to the variable length array use --= Removes the specified multiple elements from the variable length array
Example
- Define a variable length array that contains the following elements: "hadoop", "spark", "flick"
- Add a "flume" element to the variable length array
- Remove the "hadoop" element from the variable length array
- Append an array containing "hive" and "sqoop" elements to the variable length array
- Delete the two elements "spoon" and "spark" from the variable length array
- Print the array and view the results
Reference code
//Guide Package import scala.collection.mutable.ArrayBuffer //Case: modify the contents of variable length array object ClassDemo03 { def main(args: Array[String]): Unit = { //1. Define a variable length array that contains the following elements: "hadoop", "spark", "flink" val arr = ArrayBuffer("hadoop", "spark", "flink") //2. Add a "flume" element to the variable length array arr += "flume" //3. Delete the "hadoop" element from the variable length array arr -= "hadoop" //4. Append an array containing "hive" and "sqoop" elements to the variable length array arr ++= Array("hive", "sqoop") //5. Delete the two elements "spoon" and "spark" from the variable length array arr --= Array("sqoop", "spark") //6. Print the array and view the results println(s"arr: ${arr}") } }
1.4 traversal array
summary
In Scala, you can traverse an array in two ways:
- Use the index to traverse the elements in the array
- Use the for expression to directly traverse the elements in the array
Example
- Define an array containing the following elements 1,2,3,4,5 2. Traverse the array through two traversal methods and print the element reference code in the array
//Case: traversing arrays object ClassDemo04 { def main(args: Array[String]): Unit = { //1. Define an array containing the following elements 1,2,3,4,5 val arr = Array(1, 2, 3, 4, 5) //2. Traverse the array through two traversal methods and print the elements in the array //Method 1: it is implemented in the form of traversal index for(i <- 0 to arr.length -1) println(arr(i)) println("-" * 15) //Split line for(i <- 0 until arr.length) println(arr(i)) println("-" * 15) //Split line //Method 2: directly traverse the array elements for(i <- arr) println(i) } }
- Note: 0 until n gets all integers between 0 and N, including 0 and excluding n
- 0 to n gets all integers between 0 and N, including 0 and n
1.5 common array algorithms
The array in Scala encapsulates some common calculation operations. In the future, when processing data, we don't need to re implement it ourselves, but we can use it directly. The following are some commonly used algorithms:
- sum() method: Sum
- max() method: find the maximum value
- min() method: find the minimum value
- sorted() method: sort and return a new array
- reverse() method: reverse and return a new array
//Case: common algorithms for arrays object ClassDemo05 { def main(args: Array[String]): Unit = { //1. Define an array containing 4, 1, 6, 5, 2 and 3 elements val arr = Array(4, 1, 6, 5, 2, 3) //2. In the main method, test the above common algorithms //Test sum println(s"sum: ${arr.sum}") //Test max println(s"max: ${arr.max}") //Test min println(s"min: ${arr.min}") //Test sorted val arr2 = arr.sorted //That is, the contents of arr2 are: 1, 2, 3, 4, 5, 6 //Test reverse val arr3 = arr.sorted.reverse //That is, the contents of arr3 are: 6, 5, 4, 3, 2, 1 //3. Print array for (i <- arr) println(i) println("-" * 15) for (i <- arr2) println(i) println("-" * 15) for (i <- arr3) println(i) } }
2. Tuple
Tuples are generally used to store multiple values of different types. For example, tuples are used to store name, age, gender and date of birth at the same time. And the length of tuples and elements are immutable.
2.1 format
// Format 1: implemented by parentheses val/var tuple = (Element 1, Element 2, Element 3....) // Format 2: realized by arrow val/var tuple = Element 1->Element 2
2.2 example
//Case: demonstrating the definition format of tuples object ClassDemo06 { def main(args: Array[String]): Unit = { //1. Define a tuple containing the student's name and age //2. Use parentheses and arrows to define tuples val tuple1 = ("Zhang San", 23) val tuple2 = "Zhang San" -> 23 println(tuple1) println(tuple2) } }
2.3 accessing elements in tuples
In Scala, you can use tuple names_ Number to access the elements in the tuple_ 1 means to access the first element, and so on. You can also obtain the iterator of the tuple through the tuple name. productIterator, so as to traverse the tuple
format
Format I: Access a single tuple in a tuple. println(Tuple name._1) //Prints the first element of the tuple println(Tuple name._2) //Print the second tuple of the tuple ...
Format II: Traversal tuple val tuple1 = (Value 1, Value 2, Value 3, Value 4, Value 5...) //You can have multiple values val it = tuple1.productIterator //Gets the iterator object for the current tuple for(i <- it) println(i) //Print everything in the tuple
Example
//Case: get tuples in tuples object ClassDemo07 { def main(args: Array[String]): Unit = { //1. Define a tuple, including a student's name and gender, "zhangsan", "male" val tuple1 = "zhangsan" -> "male" //2. Obtain the name and gender of the student respectively //Method 1: through_ It is realized in the form of number println(s"full name: ${tuple1._1}, Gender: ${tuple1._2}") //Mode 2: through iterator traversal //Gets the iterator object corresponding to the tuple val it = tuple1.productIterator //Traversal tuples for (i <- it) println(i) } }
3. List
List is the most important and commonly used data structure in scala. In Scala, lists are divided into two types: immutable list and variable list
- Ordering does not mean sorting, but that the storage order of elements is consistent with the extraction order
- Repeatable means that duplicate elements can be added to the list
3.1 immutable list
3.1.1 features
Immutable list means that the elements and length of the list are immutable.
// Format 1: direct initialization through parentheses val/var Variable name = List(Element 1, Element 2, Element 3...) // Format 2: create an empty list through Nil val/var Variable name = Nil Format III: use :: Method implementation. use::To create a list by splicing, you must add one at the end Nil val/var Variable name = Element 1 :: Element 2 :: Nil
Example
//Case: demonstrate immutable list object ClassDemo08 { def main(args: Array[String]): Unit = { //1. Create an immutable list to store the following elements (1, 2, 3, 4) val list1 = List(1, 2, 3, 4) //2. Use 'Nil' to create an immutable empty list val list2 = Nil //3. Use the `: ` method to create a list, including - 2 and - 1 elements val list3 = -2 :: -1 :: Nil //4. Print results println(s"list1: ${list1}") println(s"list2: ${list2}") println(s"list3: ${list3}") } }
3.2 variable list
Variable list means that the elements and length of the list are variable. To use a variable list, you must use the leading package
- import scala.collection.mutable.ListBuffer
// Format 1: create an empty variable list val/var Variable name = ListBuffer[data type]() // Format 2: direct initialization through parentheses val/var Variable name = ListBuffer(Element 1, element 2, element 3...)
Example
//1. Guide Package import scala.collection.mutable.ListBuffer //Case: demonstrate variable list object ClassDemo09 { def main(args: Array[String]): Unit = { //2. Create an empty variable list val list1 = new ListBuffer[Int]() //3. Create a variable list containing the following elements: 1,2,3,4 val list2 = ListBuffer(1, 2, 3, 4) println(s"list1: ${list1}") println(s"list2: ${list2}") } }
3.2.4 common operations of variable list
Example
import scala.collection.mutable.ListBuffer //Case: demonstrate the common operation of variable list object ClassDemo10 { def main(args: Array[String]): Unit = { //1. Define a variable list containing the following elements: 1,2,3 val list1 = ListBuffer(1, 2, 3) //2. Get the first element and print the result to the console println(list1(0)) //3. Add a new element: 4 list1 += 4 //4. Add a list containing the following elements: 5, 6 and 7 list1 ++= List(5, 6, 7) //5. Delete element 7 list1 -= 7 //6. Delete elements 3, 4 list1 --= List(3, 4) //7. Convert variable list to immutable list val list2 = list1.toList //8. Convert variable list to array val arr = list1.toArray //9. Print results println(s"list1: ${list1}") println(s"list2: ${list2}") println(s"arr: ${arr}") } }
3.3 common operations of list
Example
//Case: demonstrate the basic operation of the list object ClassDemo11 { def main(args: Array[String]): Unit = { //1. Define a list list1, which contains the following elements: 1,2,3,4 val list1 = List(1, 2, 3, 4) //2. Use isEmpty method to judge whether the list is empty and print the results println(s"isEmpty: ${list1.isEmpty}") //3. Define a list list2, which contains the following elements: 4, 5 and 6 val list2 = List(4, 5, 6) //4. Use ` + + 'to splice the two lists and print the results val list3 = list1 ++ list2 println(s"list3: ${list3}") //5. Use the head method to get the first element of the list and print the results println(s"head: ${list3.head}") //6. Use the tail method to get all the elements in the list except the first element, and print the results println(s"tail: ${list3.tail}") //7. Use the reverse method to reverse the elements of the list and print the reversed results val list4 = list3.reverse println(s"list4: ${list4}") //8. Use the take method to obtain the prefix elements (the first three elements) of the list and print the results println(s"take: ${list3.take(3)}") //9. Use the drop method to obtain the suffix elements (except the first three elements) of the list and print the results println(s"drop: ${list3.drop(3)}") } }
3.3.3 example 2: flattening (flattening)
Flattening means that all concrete elements in the nested list are put into a new list separately
Example
//Case: demonstrate flattening operation object ClassDemo12 { def main(args: Array[String]): Unit = { //1. Define a list with three elements: List(1,2), List(3) and List(4,5) val list1 = List(List(1,2), List(3), List(4, 5)) //2. Use flatten to convert this list into a List(1,2,3,4,5) val list2 = list1.flatten //3. Print results println(list2) } }
3.3.4 example 3: zipper and opening
-
Zipper: combine two lists into a list whose elements are tuples
explain: Will list List("Zhang San", "Li Si"), List(23, 24)Group into list List((Zhang San,23), (Li Si,24))
-
Pull apart: disassemble a list containing tuples into tuples containing two lists
explain: Will list List((Zhang San,23), (Li Si,24))Disassembly into components(List(Zhang San, Li Si),List(23, 24))
Example
//Case: demonstrate zipper and pull object ClassDemo13 { def main(args: Array[String]): Unit = { //1. Define the list names and save the names of three students: Zhang San, Li Si and Wang Wu val names = List("Zhang San", "Li Si", "Wang Wu") //2. Define the list ages and save the ages of three students: 23, 24 and 25 val ages = List(23, 24, 25) //3. Use zip to combine the list names and ages into a list list1 with tuple elements val list1 = names.zip(ages) //4. Use unzip to disassemble list 1 into tuple tuple1 containing two lists val tuple1 = list1.unzip //5. Print results println("zipper: "+ list1) println("pull open: " + tuple1) } }
3.3.5 example 4: list to string
//Case: demonstrate converting a list into its corresponding string form object ClassDemo14 { def main(args: Array[String]): Unit = { //1. Define a list containing elements: 1,2,3,4 val list1 = List(1, 2, 3, 4) //2. Use the toString method to output the elements of the list println(list1.toString) //Short form, because: the output statement prints the object, and the toString() method of the object is called by default println(list1) println("-" * 15) //3. Use the mkString method to splice the elements with colons and print the results println(list1.mkString(":")) } }
3.3.6 example 5: Union, intersection, difference
-
Union: it means to take the union of two lists without duplication
for example: list1.union(list2), Representation acquisition list1 and list2 All elements in(Elements are not de duplicated). If you want to remove duplicate elements, You can use distinct realization.
-
intersect: indicates the intersection of two lists
for example: list1.intersect(list2), Representation acquisition list1, list2 Elements in.
-
diff: means to take the difference set of two lists
For example: list1.diff(list2),Representation acquisition list1 There are, however list2 Element not in.
Example
//Case: demonstrate obtaining Union, intersection and difference sets object ClassDemo15 { def main(args: Array[String]): Unit = { //1. The definition list list1 contains the following elements: 1,2,3,4 val list1 = List(1, 2, 3, 4) //2. The definition list list2 contains the following elements: 3,4,5,6 val list2 = List(3, 4, 5, 6) //3. Use union to get the union of the two lists val unionList = list1.union(list2) //4. Based on the third step, use distinct to remove duplicate elements val distinctList = unionList distinct //5. Use intersect to get the intersection of list1 and list2 val intersectList = list1.intersect(list2) //6. Use diff to obtain the difference set of list1 and list2 val diffList = list1.diff(list2) //7. Print results println("Union, No weight removal: " + unionList) println("Union, duplicate removal: " + distinctList) println("intersection: " + intersectList) println("Difference set: " + diffList) } }