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

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.

主站蜘蛛池模板: 会理县| 沁阳市| 石阡县| 谢通门县| 桃源县| 镶黄旗| 菏泽市| 青河县| 汾西县| 南川市| 保亭| 辽宁省| 龙游县| 长葛市| 昭平县| 长宁县| 阿巴嘎旗| 鄂州市| 固安县| 忻州市| 辽源市| 龙江县| 青冈县| 黔西| 托里县| 米泉市| 乌兰察布市| 新干县| 安远县| 六安市| 乌海市| 剑川县| 洪湖市| 五台县| 镇原县| 华坪县| 新兴县| 宜都市| 五莲县| 壶关县| 通渭县|