Scala Array Operations - Jamie Valley Big Data

one

Fixed length array:

Array of constant length, such as: declare an integer array of length 10, val arr = Array[Int](10); declare and initialize a string array: val arrStr = Array("wo","cha", "yo"). Access array method: to access the first element of arrStr, arrStr(1) can

two

Variable-length arrays (ie, array buffers):

ArrayList in Java is equivalent to ArrayBuffer in scala; but ArrayBuffer is more powerful, familiarize yourself with ArrayBuffer by doing the following:

import collection.mutable.ArrayBuffer

val arrbuff1 = ArrayBuffer[Int]()

val arrBuff2 = ArrayBuffer(1,3,4,-1,-4)

arrbuff1 += 23 //Add elements at the end with +=

arrbuff1 += (2,3,4,32) //Add multiple elements at the end at the same time

arrbuff1 ++= arrBuff2 //You can append any set with the ++= operator

arrbuff1 ++= Array(2,43,88,66)

arrbuff1.trimEnd(2) //Remove the last 2 elements

arrbuff1.remove(2) //remove arr(2+1) elements

arrbuff1.remove(2,4) //Remove 4 elements from the third element

val arr = arrbuff1.toArray //Convert the array buffer to Array

val arrbuff2 = arr.toBuffer //Convert Array to array buffer

three

Traverse arrays and array buffers:

The syntax is somewhat different on arrays and arraylists/vectors in java. Scala is more uniform. Usually, we can use the same code to handle both data structures. The for(…) yield loop creates a new collection of the same type as the original collection. You can also take guards in for loops: use if in for.

for(i <- 0 until arrbuff1.length) yield arrbuff1(i) * 2 //将得到ArrayBuffer(2,6,4,-2,-4)

for(i <- 0 until (arrbuff1.length,2)) yield arrbuff1(i) * 2 //将得到ArrayBuffer(12,-4)

for(elem <-0 arrbuff1) print(elem) //If you don't need to use subscripts, this is the easiest way

for(i <- arrbuff1 if arrbuff1 > 0) print i //Print out the integer value in arrbuff1

arrbuff1.filter( _ > 0).map{ 2 * _} //generate a new set of twice the positive numbers in arrbuff1

arrbuff1.filter map //Another way of writing

wantonly

Common Algorithms:

Scala has many convenient built-in functions, such as

arrbuff1.sum //Sum the elements of arrbuff1

Array("asd","sdf","ss").max //求最大元素

arrbuff1.sorted(_ < _) // Sort the elements of arrbuff1 from small to large

arrbuff1.sorted(_ < _) // Sort the elements of arrbuff1 from small to large

util.Sorting.quickSort(Array) //For array sorting, it is not possible to sort the array buffer

val arr = Array(1,23,4,2,45)arr.mkString(",") //Specify the separator

arr.mkString("(",",",")") //Specify prefix, separator, suffix

See Scaladoc for more functions

Wu

Multidimensional Arrays:

val matrix = Array.ofDim[Int](5,4) //A two-dimensional array with three rows and four columns

matrix(2)(3) //Access the second row and the third element

land

scala array and java interop:

Since scala arrays are implemented with java arrays, they can be passed back and forth between java and scala. By introducing scala.collectin.JavaConversion, scala buffers can be used in the code. When calling java methods, these objects will be automatically packaged into java list. Conversely, when scala.collection.asScalaBuffer is introduced, when the java method returns java.util.List, we can convert it into a Buffer.

Copyright statement: If copyright issues are involved, please contact the author with the ownership certificate

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326065944&siteId=291194637
Recommended