Quick Index
Concepts
removeAll
More Examples
Walk-Through
Concepts
The "remove" operation is basically the inverse of the "insert" operation.
Note that we do not need to worry about capacity for remove.
remove(index)
Shift to the left all elements that are to the right of "index".
Valid "index" values are from 0 to "size - 1".
remove(index)
Removal Index Position
The index that we want to remove.
Shift Left
We need to shift to the left all elements that are to the right of "index".
The remove position is "filled" with a shifted element from the right.
Algorithm
Try
to come up with an algorithm on your own.
Show Tips
This algorithm will be basically the inverse
of the
'insert' algorithm
Hint: be careful not to "wipe out"
existing data if you move data around.
removeAll
Remove all can be done easily and efficiently -- Big O(1).
All that needs to be done is to "reset" the DynamicArray -- i.e., reset the instance variables of "size" to 0 and reinitialize the fixed array using the "initialCapacity" ivar.
More Examples
Also see this...
Walk-Through
Add 10, 20, 30, 40 in that order
Remove first element by sending "removeFirst" (list is 20 30 40)
Sending method "first" should return 20 ("first" is now 20 -- see prev step)
Remove first element by sending "removeFirst" (list is 30 40)
Sending method "first" should return 30 ("first" is now 30 -- see prev step)
Do removeFirst two more times. (list is now EMPTY)
Size should be 0
isEmpty should be true
Sending method "first" should produce a runtime exception about list being empty
The other remove methods behave with a similar pattern.
Data Structures And Algorithms (DSA) (Chapter 304 - Dynamic Array)
Chapter 304 - Dynamic Array
Removing
Contents
Intro
Design
Add
Grow
Insert
Remove
Access
Design-2
Memory
Chapter
Top
Search
TOC