Arrays
An array is an ordered, indexed collection of related elements.
An array can be represented as a sequence: \(a_{0}, a_{1}, ..., a_{n}\),
where \(n\) is a positive integer greater than \(1\). The lower subscripts are indices.
One instance of this general sequence is <a,e,i,o,u>. In this case, \(n = 5\).
We can access the third vowel using the corresponding index.
If we name this sequence as \(V\), then \(V_{3}\) = i provided that indexing starts from \(1\).
We could use notations such as V[3], V(3) or Third(V).
If indexing starts from 0, then the representation is \(V_{2}\) = i.
Different programming languages use different styles for representing arrays.
Reading and modifying an element at a given index i takes time O(1).
Inserting an element at the i-th position requires (n−i) shifts and is therefore O(n−i);
likewise, deleting an element at the i-th index takes O(n−i) time.
Inserting an element into a full array requires creating a new array with greater capacity,
copying all elements from the old array, and inserting the new element.
val vowels = Array('a', 'e', 'i', 'o', 'u')
// vowels: Array[Char] = Array('a', 'e', 'i', 'o', 'u')
vowels(2)
// res0: Char = 'i'
val vowelsReversed = vowels.reverse
// vowelsReversed: Array[Char] = Array('u', 'o', 'i', 'e', 'a')
vowelsReversed(2)
// res1: Char = 'i'
vowels.length
// res2: Int = 5
Links: