1, the array

1.1 Fixed-length arrays

  • A fixed-length array means that the length of the array is not allowed to change
  • The elements of an array can be changed

grammar

// Define the array by specifying the length
val/varThe variable name =new Array[Element type](Array length)// Initialize arrays directly with elements
val/varThe variable name =Array(element1Elements,2Elements,3...)
Copy the code

Reference code

// Define the array by specifying the length
scala> val a = new Array[Int] (5)
a: Array[Int] = Array(0.0.0.0.0)

scala> a(0) = 110

scala> println(a(0))
110
// Initialize arrays directly with elements
// Define an array containing jave, Scala, and Python elements
scala> val a = Array("java"."scala"."python")
a: Array[String] = Array(java, scala, python)

scala> a.length
res17: Int = 3
Copy the code

1.2 Variable-length arrays

Variable-length arrays are arrays of variable length that can be added or removed

1.2.1 Define variable-length arrays

Create an array of variable length, need to advance import ArrayBuffer class import scala. Collection. The mutable. ArrayBuffer

grammar

  • Create an empty ArrayBuffer variable length array.

    val/var a = ArrayBuffer[Element type]()Copy the code
  • Creates an ArrayBuffer with initial elements

    val/var a = ArrayBuffer(element1Elements,2Elements,3....).Copy the code

The sample

// Define an integer variable length array of length 0
val a = ArrayBuffer[Int] ()// Define a variable length array containing the contents
scala> val a = ArrayBuffer("hadoop"."storm"."spark")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(hadoop, storm, spark)
Copy the code

1.2.2 Add/Modify/Delete Elements

  • use+ =Add elements
  • use- =Remove elements
  • use+ + =Appends an array to a variable-length array

The sample

// Define variable-length arrays
scala> val a = ArrayBuffer("hadoop"."spark"."flink")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(hadoop, spark, flink)

// Appends an element
scala> a += "flume"
res10: a.type = ArrayBuffer(hadoop, spark, flink, flume)

// Delete an element
scala> a -= "hadoop"
res11: a.type = ArrayBuffer(spark, flink, flume)

// Appends an array
scala> a ++= Array("hive"."sqoop")
res12: a.type = ArrayBuffer(spark, flink, flume, hive, sqoop)
Copy the code

1.3 Go through the number group

The array can be traversed in two ways:

  • Use the for expression to iterate directly over the elements in the array

  • Iterate over the elements in a group using an index

Reference code

// Use the 'for expression' to iterate over the elements of the array directly
scala> val a = Array(1.2.3.4.5)
a: Array[Int] = Array(1.2.3.4.5)

scala> for(i<-a) println(i)
1
2
3
4
5

// Use 'index' to iterate over the elements in the array
1,0To n -- contains0, also including n scala>for(i <- 0 to a.length - 1) println(a(i))
1
2
3
4
5
2,0Until n -- Generates a series of numbers containing0, not including n scala>for(i <- 0 until a.length) println(a(i))

Copy the code

1.4 Common algorithms for arrays

Arrays in Scala encapsulate common computation operations that we don’t need to re-implement ourselves when processing data in the future. Here are some commonly used algorithms:

  • Sum — sum method
  • Find the maximum — Max method
  • Find the minimum — min method
  • Sort — The sorted method

1.4.1 sum

The sum method in the array adds up all the elements to get the result

The sample

scala> val a = Array(1.2.3.4)
a: Array[Int] = Array(1.2.3.4)
/ / sum
scala> a.sum
res49: Int = 10
/ / Max
scala> a.max
res50: Int = 10
/ / the minimum
scala> a.min
res51: Int = 1
Copy the code

1.4.2 sorting

The sorted array method, which sorts an array in ascending order. The Reverse method, on the other hand, reverses the array to sort in descending order

The sample

// Ascending sort
scala> a.sorted
res53: Array[Int] = Array(1.2.4.4.10)

/ / descending
scala> a.sorted.reverse
res56: Array[Int] = Array(10.4.4.2.1)
Copy the code