An array is collection of items stored at contiguous memory locations. The idea is to store multiple items of same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on. Remember: “Location of next index depends on the data type we use”. The above image can be looked as a top-level view of a staircase where you are at the base of staircase. Each element can be uniquely identified by their index in the array (in a similar way as you could identify your friends by the step on which they were on in the above example).
- Stored in a block of continuous memory.
- Brother of the Array is the Linked List.
- Index associated with memory address.
- Implemented as a struct in Swift
- Implement mathematical matrices and vectors.
- Many databases, small and large, consist of (or include) one-dimensional arrays whose elements are records.
- Arrays are used to implement other data structures, such as lists, heaps, hash tables, deques, queues, stacks, strings, and VLists.
- Access in linear time, O(1)
- Good for elements of fixed size
- Allows for random access of elements
- Cache friendly, easy to copy
- Increasing size of array takes extra time
- Append and remove can take O(n) for a sorted list.