Overview
TheArray class provides a static array implementation with a fixed capacity. Unlike Python’s built-in list, this array has a predetermined maximum size and will raise exceptions if capacity is exceeded.
Special Methods
- Index Operator:
array[index]- Access elements by index - Assignment:
array[index] = value- Set values at specific indices - Equality: Array instances can be compared for equality with other
ArrayorDynamicArrayinstances based on their contents
Constructor
__init__(contents=None, capacity: int=10)
Initialize the array with optional contents and a fixed 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. Determines the maximum number of elements the array can hold.
Methods
append(element)
Append an element to the end of the array.
The element to append to the array.
Exception: If the array is full and capacity is exceeded
extend(array)
Append multiple elements from a given iterable.
An iterable containing elements to append to the array.
Exception: If appending the elements exceeds the array’s capacity
insert(index: int, element)
Insert an element at a specified index, shifting existing elements to the right.
The index at which to insert the element. Must be between 0 and the current count (inclusive).
The element to insert.
IndexError: If the index is out of boundsException: If the array is full and cannot accommodate the shift
delete(index: int)
Delete an element at a specified index, shifting subsequent elements to the left.
The index of the element to delete. Must be between 0 and count - 1.
IndexError: If the index is out of bounds
shift_right(start: int)
Helper method to shift elements to the right from a specified start index until the last element. This is primarily used internally by the insert method.
The index at which to start shifting (inclusive).
Exception: If the array is full and cannot accommodate the shift
Note
Note
This method may delete an element but does not affect the count. It is typically used internally and not called directly by users.
shift_left(start: int)
Helper method to shift elements to the left starting at a start index. This is primarily used internally by the delete method.
The starting index of the shift.
Note
Note
This method may delete an element but does not affect the count. It is typically used internally and not called directly by users.
is_empty() -> bool
Check if the array is empty.
Returns
True if the array contains no elements, False otherwise.capacity() -> int
Get the total capacity of the array.
The maximum number of elements the array can hold.
to_list() -> list
Convert the array’s elements to a standard Python list.
A list containing only the active elements of the array (not including unused capacity).
from_list(mylist: list) (classmethod)
Create an array from a standard Python list.
A Python list to initialize the array with.
An instance of the Array class with capacity equal to the length of the input list.
Special Methods
__getitem__(index: int)
Retrieve the element at the specified index. Enables the use of bracket notation array[index].
The index of the element to retrieve. Must be between 0 and count - 1.
The element at the specified index.
IndexError: If the index is out of bounds
__setitem__(index: int, value)
Set a new value at the specified index. Enables the use of bracket notation array[index] = value.
The index at which to set the value. Must be between 0 and count - 1.
The new value to assign.
IndexError: If the index is out of bounds
__len__() -> int
Return the number of elements in the array. Enables the use of len(array).
The number of active elements in the array (not the capacity).
__repr__() -> str
Represent the array’s contents, count, and capacity as a string.
A string representation showing the array’s elements, current count, and total capacity.
__eq__(other) -> bool
Compare this array to another for equality.
The object to compare with.
Returns
True if both objects are Array, DynamicArray, or CircularArray instances and their contents are equal. For non-array types, returns NotImplemented to allow reverse comparison.Attributes
count
The number of elements currently stored in the array. This is updated automatically when elements are added or removed.