官术网_书友最值得收藏!

Array

The array provides fast and constant time performance for the random access (index-based access) of a collection. Arrays can be thought of as a data structures that are backed by an array of bytes that are contiguously laid out in the computer's memory. This means that individual elements of an array are placed one after the other in memory. With a layout like this, there is an implicit connection between the elements and the physical position of the elements in the memory, determining which one is first, next, and so on. For example, the position of the tenth element in an array can be determined by adding the sizes of the first nine elements.

The following is an example run execution in Scala REPL to demonstrate the Scala array and some of its functionality:

scala> val persons = Array(Person("Jon", "Doe", 21), Person("Alice", "Smith", 20), Person("Bob", "Crew", 27)) // construct a Array of Person objects
persons: Array[Person] = Array(Person(Jon,Doe,21), Person(Alice,Smith,20), Person(Bob,Crew,27))

scala> val personHead = persons.head // first person in the collection
personHead: Person = Person(Jon,Doe,21)

scala> val personAtTwo = persons(2) // person at index 2 (this is same as apply operation)
personAtTwo: Person = Person(Bob,Crew,27)

scala> val personsTail = persons.tail // collection without the first person
personsTail: Array[Person] = Array(Person(Alice,Smith,20), Person(Bob,Crew,27))

scala> val personsByAge = persons.sortBy(p => p.age) // sort persons by age
personsByAge: Array[Person] = Array(Person(Alice,Smith,20), Person(Jon,Doe,21), Person(Bob,Crew,27))

scala> val personsByFname = persons.sortBy(p => p.fname) // sort persons by first name
personsByFname: Array[Person] = Array(Person(Alice,Smith,20), Person(Bob,Crew,27), Person(Jon,Doe,21))

scala> val (below25, above25) = persons.partition(p => p.age <= 25) // split persons by age
below25: Array[Person] = Array(Person(Jon,Doe,21), Person(Alice,Smith,20))
above25: Array[Person] = Array(Person(Bob,Crew,27))

scala> val updatePersons = persons.updated(0, Person("Jon", "Doe", 20)) // update first element
updatePersons: Array[Person] = Array(Person(Jon,Doe,20), Person(Alice,Smith,20), Person(Bob,Crew,27))

The following is a summary of the array operations and their associated performance characteristics:

As can be seen in the preceding table, the apply operation for getting the element at a specified index is a fast constant-time operation for an array. Along similar lines, the update operation for replacing an element at the specified index is also a fast constant-time operation. On the other hand, the tail operation for getting elements other than the head is a slow linear time operation. In fact, the prepend, append, and insert operations are not even supported for an array. This might seem a limiting factor at first, but Scala has an ArrayBuffer class for building an array, and that should be used if such operations are necessary.

In data analysis, we typically create a dataset initially and use it over and over again during different phases of the analysis. This implies that the dataset construction is generally a one-time step, and the construction is then used multiple times. This is precisely why a builder such as ArrayBuffer is separated from the array: because each serves a different purpose. The ArrayBuffer is designed to help in the construction of the array with support for the commonly desired build operations. Let's look at the ArrayBuffer functionality using Scala REPL, as shown in the following code:

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val personsBuf = ArrayBuffer[Person]() // create ArrayBuffer of Person
personsBuf: scala.collection.mutable.ArrayBuffer[Person] = ArrayBuffer()
scala> personsBuf.append(Person("Jon", "Doe", 21)) // append a Person object at the end
scala> personsBuf.prepend(Person("Alice", "Smith", 20)) // prepend a Person object at head
scala> personsBuf.insert(1, Person("Bob", "Crew", 27)) // insert a Person object at index 1
scala> val persons = personsBuf.toArray // materialize into an Array of Person
persons: Array[Person] = Array(Person(Alice,Smith,20), Person(Bob,Crew,27), Person(Jon,Doe,21))
scala> val personRemoved = personsBuf.remove(1) // remove Person object at index 1
personRemoved: Person = Person(Bob,Crew,27)

scala> val personsUpdated = personsBuf.toArray // materialize into an Array of Person
personsUpdated: Array[Person] = Array(Person(Alice,Smith,20), Person(Jon,Doe,21))

As can be seen in the preceding code, ArrayBuffer provides a comprehensive set of functionalities to construct a collection and provides a convenient mechanism to materialize it into an array once construction is complete.

主站蜘蛛池模板: 岳普湖县| 呼伦贝尔市| 石河子市| 冕宁县| 勃利县| 乾安县| 瑞安市| 河西区| 廊坊市| 汪清县| 闻喜县| 大名县| 凤冈县| 荆门市| 通化县| 彰武县| 贵南县| 大关县| 祁门县| 河北省| 安远县| 综艺| 青川县| 庄河市| 噶尔县| 五常市| 正阳县| 屏边| 威海市| 芦山县| 德惠市| 庄河市| 六安市| 巴中市| 夏河县| 修文县| 合山市| 申扎县| 韩城市| 手游| 德清县|