Overview
TheDynamicArray class extends the Array class to provide a dynamic array implementation with automatic capacity management. Unlike the static Array, the DynamicArray automatically grows when capacity is exceeded and shrinks when utilization is low.
Special Methods
- Index Operator:
array[index]- Access elements by index - Assignment:
array[index] = value- Set values at specific indices - Equality: DynamicArray instances can be compared for equality with other
DynamicArrayorArrayinstances based on their contents
Inheritance
DynamicArray inherits from Array and overrides key methods to provide automatic capacity management:
- Inherits:
__getitem__,__setitem__,__len__,__repr__,__eq__,is_empty,capacity,to_list,from_list,shift_right,shift_left - Overrides:
append,extend,insert,delete - Adds:
grow,shrink,check_capacity
Constructor
__init__(contents=None, capacity: int=10)
Initialize the dynamic array with optional contents and an initial capacity.
An optional iterable to fill the array with initial values. If the length of contents exceeds the specified capacity, the capacity will be automatically adjusted to fit.
The initial size of the array. The capacity will automatically grow or shrink as needed.
Note
Note
The constructor is inherited from the
Array class. The key difference is that the capacity will dynamically adjust during the lifetime of the array.Capacity Management Methods
grow()
Helper method to double the capacity of the current array.
Time Complexity: O(n) where n is the current number of elements
Space Complexity: O(n) for the new array allocation
Implementation Details
Implementation Details
This method creates a new internal array with double the capacity and copies all existing elements to it. It is automatically called by
check_capacity() when needed.shrink()
Helper method to halve the capacity of the current array. The minimum capacity is 10.
Time Complexity: O(n) where n is the current number of elements
Space Complexity: O(n) for the new array allocation
Implementation Details
Implementation Details
This method creates a new internal array with half the capacity and copies all existing elements to it. The capacity will never shrink below 10. It is automatically called by
check_capacity() when needed.check_capacity()
Automatically adjusts the array capacity based on utilization:
- If
count >= capacity, grow the array - If
count <= 1/4 of capacity, shrink the array
Amortized Analysis
Amortized Analysis
While individual resize operations take O(n) time, the amortized cost per operation is O(1) because resizing happens infrequently. Each resize doubles or halves the capacity, so the number of resizes is logarithmic in the total number of operations.
Methods (Overridden)
append(element)
Append an element to the array. Automatically adjusts capacity as needed.
The element to append to the array.
Difference from Array
Difference from Array
Unlike the static
Array.append() which raises an exception when capacity is exceeded, DynamicArray.append() automatically grows the array to accommodate new elements.extend(array)
Append multiple elements from a given iterable. Automatically adjusts capacity as needed.
An iterable containing elements to append to the array.
Difference from Array
Difference from Array
Unlike the static
Array.extend() which may raise an exception when capacity is exceeded, DynamicArray.extend() automatically grows the array as needed.insert(index: int, element)
Insert an element at a specified index, shifting existing elements to the right. Automatically adjusts capacity as needed.
The index at which to insert the element. Must be between 0 and count - 1 (not count as in Array).
The element to insert.
IndexError: If the index is out of bounds
Difference from Array
Difference from Array
Unlike
Array.insert(), this method automatically grows the array if needed. Note that the index validation is also different: it must be strictly less than count, not less than or equal to.delete(index: int)
Delete an element at a specified index, shifting subsequent elements to the left. Automatically adjusts capacity as needed.
The index of the element to delete. Must be between 0 and count - 1.
IndexError: If the index is out of bounds
Difference from Array
Difference from Array
Unlike
Array.delete(), this method automatically shrinks the array when utilization falls below 25% of capacity.Inherited Methods
The following methods are inherited from theArray class and work identically:
Index Access
__getitem__(index: int)- Retrieve element at index usingarray[index]__setitem__(index: int, value)- Set value at index usingarray[index] = value
Utility Methods
is_empty() -> bool- Check if the array is emptycapacity() -> int- Get the current total capacityto_list() -> list- Convert to a standard Python listfrom_list(mylist: list)- Create a DynamicArray from a Python list (classmethod)
Helper Methods
shift_right(start: int)- Shift elements right from a start indexshift_left(start: int)- Shift elements left from a start index
Special Methods
__len__() -> int- Get the number of elements usinglen(array)__repr__() -> str- String representation of the array__eq__(other) -> bool- Compare arrays for equality
View Full Documentation
View Full Documentation
For detailed documentation of inherited methods, see the Array reference page.
Attributes
count
The number of elements currently stored in the array. This is updated automatically when elements are added or removed.
Performance Characteristics
Time Complexity Summary
| Operation | Average Case | Worst Case |
|---|---|---|
append | O(1) | O(n) |
extend | O(m) | O(n + m) |
insert | O(n) | O(n) |
delete | O(n) | O(n) |
__getitem__ | O(1) | O(1) |
__setitem__ | O(1) | O(1) |
is_empty | O(1) | O(1) |
capacity | O(1) | O(1) |
Space Complexity
TheDynamicArray maintains O(n) space complexity where n is the number of elements stored. The actual capacity may be up to 4x the number of elements before shrinking occurs.