Static and Dynamic Arrays: William Fiset
Static and Dynamic Arrays: William Fiset
Dynamic Arrays
William Fiset
Outline
• Discussion and examples about Arrays
• What is an Array?
• When and where is a Array used?
• Complexity
• Static array usage example
• Dynamic Array implementation details
• Code Implementation
Discussion and
examples
What is a static
Array?
A static array is a fixed length
container containing n elements
indexable from the range [0, n-1].
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
A[0] = 44
A[1] = 12
A[4] = 6
A[7] = 9
A[9] => index out of bounds!
Static Array
A = -1 12 -5 17 6 0 3 9 100
0 1 2 3 4 5 6 7 8
A[0] = 44 A[0] := -1
A[1] = 12
A[4] = 6
A[7] = 9
A[9] => index out of bounds!
Static Array
A = -1 12 -5 17 6 18 3 9 100
0 1 2 3 4 5 6 7 8
A[0] = 44 A[0] := -1
A[1] = 12 A[5] := 18
A[4] = 6
A[7] = 9
A[9] => index out of bounds!
Static Array
A = -1 12 -5 17 6 18 25 9 100
0 1 2 3 4 5 6 7 8
A[0] = 44 A[0] := -1
A[1] = 12 A[5] := 18
A[4] = 6
A[6] := 25
A[7] = 9
A[9] => index out of bounds!
Operations on
Dynamic Arrays
Dynamic Array
The dynamic array can grow and
shrink in size.
A = 34 4
A.add(-7) A = 34 4 -7
A.add(34) A = 34 4 -7 34
A.remove(4) A = 34 -7 34
Dynamic Array
Q: How can we implement a dynamic array?
A: One way is to use a static array!
Ø Ø 7 Ø 7 -9
7 -9 3 Ø 7 -9 3 12
7 -9 3 12 5 Ø Ø Ø
7 -9 3 12 5 -6 Ø Ø