Python defines both an array and a list type and while they look and feel similar to one another they have their own uses. The array.array class in Python is a thin wrapper around a C array and this introduces some limitations. For example, Python arrays are homogeneous and can only hold data of a single kind. The type does take up much less space in memory than Python lists however so in general you would use an array if space was a concern or if you wanted to expose some C functionality.
As mentioned in the course, Python lists are the more frequently used type and the de facto representation of an array like structure. They are a heterogeneous and contiguous data structure.
Arrays in Java are represented by the Array class and is a homogeneous container containing a fixed number of values of a single type. Two types of arrays can be created - one containing primitive values like int or char or ones that contain objects by means of references or pointers.
To declare a variable to refer to an array the type must be specified.
// declares an array of integers
Declaring an array does not actually create the array, instead it indicates that the variable holds an array of the type that we specify. When an array is created the variable anArray holds a reference to the location in memory where the array is stored.
When arrays of primitive values are created, the values are stored in the array. For objects however, the array contains references to the locations in memory where the objects are stored.
There are several ways to create an array:
var arr = new Array(1);
var arr = Array(1);
var arr = ;
Consider the following snippet:
var values = ;
values = 1;
values = 2;
Here an array has been created and two values have been inserted at index positions 0 and 9. The length of the array here is actually 10, even though there are only 2 elements in the array.
The Array type in Swift is a generic, homogenous data structure containing values of a single type and is the de facto list data structure. When creating an array the type of the elements can be inferred, making type specification optional.
let oddNumbers = [3, 5, 7] // Type inferred
let evenNumbers: [Int] = [2, 4, 6] // Explicit type annotation
In addition, arrays can be created using the literal syntax or by specifying the generic type parameters directly. To create an empty array:
let vowels: [String] = 
let consonants = Array<String>()
When creating an empty array using the literal syntax the type must be explicitly specified.
When an array is created a specific amount of memory is allocated to store the values. As the array begins to exceed this reserved capacity, a larger region of memory that is a multiple of the original size is allocated and the values are copied over into the new memory. This exponential growth strategy means that appending an element happens in constant time, averaging the performance of many append operations.
If the desired size of the array is known beforehand, the reserveCapacity(_:) method can be used before appending to the array to avoid intermediate reallocations.
Array storage can be either in contiguous or non-contiguous memory. If the underlying element is a value type then the memory allocated is guaranteed to be contiguous but if the values stored are references types (i.e, classes), then the memory can either be contiguous or an instance of NSArray if the objects stored are @objc types.
Contiguous memory can be guaranteed by using the ContiguousArray type.
Arrays in Swift are value types and implement copy-on-write behavior. Multiple copies of an array share the same storage until one of them is modified. When a copy is modified, the array being modified replaces its (shared) storage with a uniquely owned copy of itself.
When storing value types, the value is stored directly in the array's storage. On the other hand, when reference types are stored, the array stores a pointer to the location in memory where the object is stored.