ArrayList Flashcards
What is a Array?
Arrays hold values of the same type at contiguous memory locations.
What are we concerned about in a Array?
The position/index of an element and the element itself.
Advantages
-Store multiple elements of the same type with one single variable name
-Accessing elements is fast as long as you have the index, as opposed to linked lists where you have to traverse from the head.
Disadvanteges
-Addition and removal of elements into/from the middle of an array is slow because the remaining elements need to be shifted to accommodate the new/missing element. An exception to this is if the position to be inserted/removed is at the end of the array.
-For certain languages where the array size is fixed, it cannot alter its size after initialization. If an insertion causes the total number of elements to exceed the size, a new array has to be allocated and the existing elements have to be copied over. The act of creating a new array and transferring elements over takes O(n) time.
Subarray
A range of contiguous values within an array.
Example: given an array [2, 3, 6, 1, 5, 4], [3, 6, 1] is a subarray while [3, 1, 5] is not a subarray.
Subsequence
A sequence that can be derived from the given sequence by deleting some or no elements without changing the order of the remaining elements.
Example: given an array [2, 3, 6, 1, 5, 4], [3, 1, 5] is a subsequence but [3, 5, 1] is not a subsequence
Insertion
Insert O(n) Insertion would require shifting all the subsequent elements to the right by one and that takes O(n)
Access , Search , Search (sorted array)
Access O(1)
Search O(n)
Search (sorted array) O(log(n))
Insert (at the end)
O(1) Special case of insertion where no other element needs to be shifted
Remove
O(n) Removal would require shifting all the subsequent elements to the left by one and that takes O(n)
Remove (at the end)
O(1) Special case of removal where no other element needs to be shifted
Why do we need arrays?
Arrays are best for storing multiple values in a single variable
Arrays are better at processing many values easily and quickly
Sorting and searching the values is easier in arrays