Static or dynamic collections
Fixed-size array versus resizable list: capacity and size, appending, insertion, deletion, the cost of growing and choosing according to the need.
- 24 cards
- 6 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The numbers
- Scenarios
- Understand
A few cards from the deck
Fixed-size array: what is it?
Show the answer
A sequence of cells whose number is decided at creation and never changes: the space is reserved in a single block.
In C,
int t[5];reserves five integers for the whole life of the array; in Java,new int[5]likewise. A sixth value does not fit.What does this program print?
```python
t = [4, 8]
t.append(1)
t.insert(1, 6)
t.pop()
t.remove(4)
print(t)`[6, 8][4, 6, 8][6, 8, 1][6, 1]
Show the answer
[6, 8][4, 8], thenappend(1)gives[4, 8, 1],insert(1, 6)gives[4, 6, 8, 1],pop()removes the last one (1),remove(4)removes the value 4:[6, 8]is left.In a dynamic list,
len(t)gives the …, that is the number of elements, which always stays lower than or equal to the ….Show the answer
In a dynamic list,
len(t)gives the size, that is the number of elements, which always stays lower than or equal to the capacity.A list of capacity 8 holding 5 elements has a size of 5. The three free cells are ready for the next appends, with no copying.
Dynamic list: what is it?
Show the answer
A sequence that grows and shrinks as items are added and removed, usually reserving more cells than it uses.
It is Python's
listand Java'sArrayList. Underneath there is always a fixed-size array, replaced by a larger one when it is full.