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

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.

主站蜘蛛池模板: 大化| 南和县| 绥棱县| 黔江区| 建平县| 芜湖市| 裕民县| 奉新县| 基隆市| 灵武市| 广水市| 涿鹿县| 白城市| 鹤庆县| 十堰市| 永胜县| 阿瓦提县| 嘉定区| 克东县| 凤阳县| 镇雄县| 南木林县| 台中市| 富川| 黎城县| 雷山县| 林周县| 鹿邑县| 台东市| 三亚市| 扶沟县| 布尔津县| 红原县| 鹤岗市| 饶平县| 玉屏| 田阳县| 建阳市| 泸水县| 唐海县| 泰兴市|