Visualize common array operations: Access, Insertion, and Deletion.
Enter an initial array or perform an operation.
address = base_address + index * element_size
.
class ArrayWrapper {
constructor() {
this.array = [];
}
access(index) {
if (index < 0 || index >= this.array.length) {
throw new Error("Index out of bounds");
}
// Direct access: O(1)
return this.array[index];
}
insert(index, value) {
if (index < 0 || index > this.array.length) { // Allow inserting at the end
throw new Error("Index out of bounds");
}
// Shift elements: O(n)
this.array.splice(index, 0, value);
}
delete(index) {
if (index < 0 || index >= this.array.length) {
throw new Error("Index out of bounds");
}
// Shift elements: O(n)
this.array.splice(index, 1);
}
}