Array
Like an array of Java, it is also an immutable arrays, and since both Scala and Java are running in the JVM, both sides can call each other, so the bottom of the Scala array is actually a Java array.
Note: Accessing an array of elements using () instead of [] in Java
Scala>ValA =NewArray[string] (Ten) a:array[string] = Array (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, null) scala>ValA =NewArray[boolean] (Ten) A:array[boolean] = Array (false,false,false,false,false,false,false,false,false,false) scala>a(0) Res15:boolean =falseScala>ValA =NewArray[int] (Ten) A:array[int] = Array (0,0,0,0,0,0,0,0,0,0) scala>a(0) =1
?
An array can be created directly using array (), and the element type is automatically inferred ( public parent type if the type is inconsistent )
Scala>ValA = Array ("Hello","World") a:array[string] = Array (Hello, World) scala>ValA = Array ("Sparks", -) A:array[any] = Array (Sparks, -)//Common operationsScala>ValA = Array (3,4,1,2,5,3) A:array[int] = Array (3,4,1,2,5,3) scala>Valsum = A.sumSum:int = -Scala>ValMax = A.MaxMax:int =5Scala> Scala.util.sorting.QuickSort(a) scala> ares35:array[int] = Array (1,2,3,3,4,5) scala> A.mkstringRes36:string =123345Scala> A.mkstring(",") res37:string =1,2,3,3,4,5Scala> A.mkstring("<",",",">") Res38:string = <1,2,3,3,4,5>//The ToString of array has some problemsScala> A.toStringres39:string = [email protected]scala> b.toStringres40:string = ArrayBuffer (1,6,7,8,9,Ten)
?
ArrayBuffer
ArrayList-length Variable collection class similar to Java
Scala>ImportScala.Collection.mutable.ArrayBufferImportScala.Collection.mutable.ArrayBufferScala>Valb = Arraybuffer[int] () B:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer ()//Use + = To add one or more elements, spark the source of a lot of use!!! Scala> B + =1Res17:b.type= ArrayBuffer (1) scala> B + = (2,3,4,5) res18:b.type= ArrayBuffer (1,2,3,4,5)//Use ++= to add all elements from another collectionScala> b ++= Array (6,7,8,9,Ten) res19:b.type= ArrayBuffer (1,2,3,4,5,6,7,8,9,Ten)the//TrimEnd method can truncate the specified number of elements from the tailScala> B.trimEnd(5) scala> Bres21:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer (1,2,3,4,5)//Use the Insert () function to insert one or more elements at a specified location, which is less efficientScala> B.Insert(5,6) Scala> B.Insert(6,7,8,9,Ten) scala> Bres24:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer (1,2,3,4,5,6,7,8,9,Ten)//Use the Remove () function to remove one or more elements from a specified positionScala> B.Remove(1) Res25:int =2Scala> B.Remove(1,3) scala> Bres27:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer (1,6,7,8,9,Ten)//Array and Arraybuffer convert each otherScala> B.ToArrayRes28:array[int] = Array (1,6,7,8,9,Ten) scala> A.TobufferRes29:scala.Collection.mutable.Buffer[Any] = ArrayBuffer (Sparks, -)
?
Traversing arrays and Arraybuffer
Using until is a function provided by Richint
for (i <-0 until B.length)
Print (b (i))
Jump Traversal, Step length
for (i <-0 until (b.length, 2))
Print (b (i))
Traverse from tail
for (I <-(0 until b.length). Reverse)
println (b (i))
Using the enhanced for loop traversal
For (e <-b)
println (e)
?
Array conversion of array operations
- Use yield
Scala>ValA = Array (1,2,3,4,5) A:array[int] = Array (1,2,3,4,5) scala>ValA2 = for(Ele <-a)yieldEle*elea2:array[int] = Array (1,4,9, -, -) scala>ValA = ArrayBuffer (1,2,3,4,5) A:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer (1,2,3,4,5) scala>ValA2 = for(Ele <-a)yieldEle*elea2:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer (1,4,9, -, -)
- Using functional programming
_ Denotes a wildcard character
- A.filter (_% 2 = = 0). Map (2 * _) (Recommended method)
- a.filter{_% 2 = = 0} map {2 * _}
?
Combat: Remove all negative numbers after the first negative number
//Build ArraysScala>ValA = Arraybuffer[int] () A:scala.Collection.mutable.ArrayBuffer[Int] = ArrayBuffer () scala> A + = (1,2,3,4,5,-1,-3,-5,-9) res45:a.type= ArrayBuffer (1,2,3,4,5, -1, -3, -5, -9)//Find the negative number after the first negative number, remove it, perform poorly, move the array multiple timesvarFoundfirstnegative =falsevarindex =0 while(Index < A.length) {if(a(index) >=0) {Index + =1}Else{if(!foundfirstnegative) {foundfirstnegative=true; Index + =1}Else{A.Remove(index)} }}//Retrieve all required elements, then move all required elements to the left of the array and delete the unwanted elements on the right. //modified version, high performancevarFoundfirstnegative =falseValVaildindex = for(I <-0Until a.length if(!foundfirstnegative | |a(i) >=0))yield{if(a(i) <0) Foundfirstnegative =trueI for(I <-0Until Vaildindex.length) {a(i) =a(Vaildindex(i))} A.trimEnd(A.length-Vaildindex.length)
Scala Entry Series (c): arrays