- Hands-On Data Analysis with Scala
- Rajesh Gupta
- 639字
- 2021-06-24 14:51:06
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.
- JMAG電機(jī)電磁仿真分析與實(shí)例解析
- INSTANT Varnish Cache How-to
- JSF2和RichFaces4使用指南
- 大型數(shù)據(jù)庫(kù)管理系統(tǒng)技術(shù)、應(yīng)用與實(shí)例分析:SQL Server 2005
- 統(tǒng)計(jì)策略搜索強(qiáng)化學(xué)習(xí)方法及應(yīng)用
- 21天學(xué)通Java
- 數(shù)據(jù)掘金
- 工業(yè)機(jī)器人維護(hù)與保養(yǎng)
- 電子設(shè)備及系統(tǒng)人機(jī)工程設(shè)計(jì)(第2版)
- Excel 2010函數(shù)與公式速查手冊(cè)
- 簡(jiǎn)明學(xué)中文版Photoshop
- 21天學(xué)通Linux嵌入式開(kāi)發(fā)
- 智慧未來(lái)
- 計(jì)算機(jī)辦公應(yīng)用培訓(xùn)教程
- 渲染王3ds Max三維特效動(dòng)畫技術(shù)