Scala Entry Series (c): arrays

Source: Internet
Author: User

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
    1. Using until is a function provided by Richint
      for (i <-0 until B.length)
      Print (b (i))

    2. Jump Traversal, Step length
      for (i <-0 until (b.length, 2))
      Print (b (i))

    3. Traverse from tail
      for (I <-(0 until b.length). Reverse)
      println (b (i))

    4. Using the enhanced for loop traversal
      For (e <-b)
      println (e)

?

Array conversion of array operations
    1. 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, -, -)
    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.