Data Structures: Arrays
- An array is a data structure which allows storage of a collection of data
- It is a Linear data structure
- A block of consecutive bytes is allocated in memory for the array
Speed of Arrays
Arrays can access data fast if you have the index, this is due to the data being stored consecutively in memory.
32bit int Array Example in memory
You can easily find any index. eg index 0 starts at 100 and you want to find index 3, then index0(100) + (index(3)*bytesize(4)) ie 100+(3*4) => goto memory 112
Arrays have a set size array because after the specified memory has been allocated something else may have been allocated in the memory just after the array, not allowing it to extend.
Since the array was created with a size of 8 it only reserves enough space for 8, therefore it can not extend because something else may use the memory after its allocated memory.
What to do if you need a larger array
A strategy to resize arrays is to create a new larger array and copy the old data over.
eg start with array then when its full create array and copy the data across.
Many languages have this resizing feature built into some of their datatypes.
- C#: ArrayList, List, StringBuilder
- Python: list
- Java: ArrayList