Programming technique · B2.2.1, B2.2.2
Arrays and lists
Lists and indexed data
Arrays and lists store several related values under one variable name. An index identifies each position, beginning at zero. The important design choice is whether capacity is fixed or allowed to change.
Python lists store several related values under one variable name. An index identifies each position, beginning at zero. Python lists are dynamic internally, although a program can deliberately use a fixed number of positions.
Static versus dynamic: what actually changes?
| Question | Fixed array | Dynamic ArrayList |
|---|---|---|
| Capacity | Length is fixed when the array is created. | Logical size can grow and shrink. |
| Resizing mechanism | To obtain a different capacity, create another array and move/copy values. | The implementation can obtain larger internal storage when more capacity is needed; a resize can require allocation and copying. |
| Speed/predictability | Simple fixed-capacity indexed storage; no automatic resizing step. | Convenient growth, but an occasional resize can cost extra work. |
| Memory | Unused fixed positions may waste capacity if the estimate is too large. | May keep spare capacity and has management overhead to support growth. |
| Flexibility | Best when the required capacity is known and stable. | Best when the number of elements genuinely changes. |
| Question | Fixed-capacity use of a list | Dynamic list use |
|---|---|---|
| Capacity behaviour | The program creates a chosen number of positions and deliberately avoids adding/removing. | The program uses append/insert/pop/remove as the number of elements changes. |
| Underlying structure | A Python list is still a dynamic structure internally; “fixed-capacity use” describes the program's rule, not a separate static Python array type. | |
| Resizing mechanism | No logical resize is requested by the program. | The implementation can obtain more internal capacity as the list grows; resizing may involve additional allocation/movement of stored references. |
| Memory | Reserved placeholder positions can remain unused. | Dynamic growth provides flexibility but may keep spare capacity and management overhead. |
| Flexibility | Useful when position meanings and capacity must stay stable. | Useful when the number of stored elements changes during execution. |
Fixed-size arrays
An array has a fixed length after creation. It is appropriate when the number of positions is known and stable.
Indexed lists
A Python list can represent a known set of indexed positions even though the list type itself can grow or shrink.
The final valid index is always one less than the collection length.
Dynamic ArrayLists: add, remove and traverse
An ArrayList can grow and shrink. Use the generic type to state what each element stores. Adding or removing an element can also shift later positions.
Dynamic lists: add, remove and traverse
A list can grow with append() and shrink with pop() or remove(). Every later index may shift after an element is removed.
Two-dimensional arrays
Two-dimensional lists
A two-dimensional structure uses a row index and a column index.
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed after creation | Can grow and shrink |
| Length | array.length | list.size() |
| Read | array[index] | list.get(index) |
| Replace | array[index] = value | list.set(index, value) |
| Add/remove | Not directly | add() and remove() |
| Feature | Fixed-capacity use | Dynamic list use |
|---|---|---|
| Size | Program keeps the original number of positions | Program may grow or shrink the list |
| Length | len(values) | len(values) |
| Read | values[index] | values[index] |
| Replace | values[index] = value | values[index] = value |
| Add/remove | Avoided by design | append(), insert(), pop(), remove() |
Check your understanding
Answer each question before opening the model answer.
-
A program will always store exactly 12 monthly totals. In Java, which structure is the natural first choice?
Reveal model answer
A fixed array is a natural fit because the required capacity is known and does not change.
-
A waiting list can gain and lose names throughout the day. Which behaviour matters most?
Reveal model answer
Dynamic growth and shrinkage, because the number of stored elements changes during execution.
-
Why can resizing a dynamic structure occasionally require extra work?
Reveal model answer
The implementation may need to obtain larger backing storage and move/copy existing stored values or references into it.
-
Is a Python list a truly static data structure when you decide not to append to it?
Reveal model answer
No. The Python list type is dynamic; the program is merely imposing a fixed-capacity usage rule.
Index boundaries
Valid indexes run from 0 to length - 1.
Match related data carefully
Parallel collections only work when corresponding values stay at the same index.
Choose the structure deliberately
Use an array for stable fixed capacity and an ArrayList when the number of elements changes.
Choose the behaviour deliberately
Python lists are dynamic, but the problem may still require stable positions or controlled growth.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Text Dice
SelectedStore the words one, two, three, four, five and six in an array. Generate a random index and output the matching word without using an if or switch statement to translate the number.
Notebook
SelectedCreate a notebook for up to 10 notes. Decide whether a fixed array or a dynamic ArrayList better fits your design, implement the chosen structure, repeatedly display the numbered notes, allow a valid position to be edited and allow the user to quit. Explain the flexibility and memory/capacity trade-off behind your choice.
Currency Converter
SelectedStore at least five currency names and exchange rates in matching arrays or ArrayLists. Ask for an amount in British pounds and a target currency, then output the selected rate and converted amount. Keep each name and rate at matching indexes and handle an unknown currency clearly.
One-Dimensional Battleships
SelectedCreate a one-player Battleships game using a board with positions 1 to 50. Randomly place five single-position ships without duplicates. The player keeps guessing until every ship is found. Report hits, misses and total attempts, and prevent the same position being guessed twice.
Optional extension: make each ship occupy several adjacent positions while keeping every ship inside the board boundaries. Do not build the full six-part chain unless your teacher requests it.
Selected challenge
This choice is shared with the portfolio setup page.
Plan your solution in handwritten pseudocode
Before opening your IDE or writing any program code, handwrite pseudocode for this challenge on paper.
Not marked complete. If you submit now, the GitHub README will record “No”.
Create your challenge folder
Run this command after planning. It creates the correct empty folder inside your portfolio.
Complete the challenge
Use your handwritten pseudocode as the starting plan, then write and test your solution in the folder created above.
Optional two-level scaffold
Try from your handwritten pseudocode first. Scaffold gives some structure; Scaffold + comments gives stronger guidance. Use only the level you need, and update your pseudocode first if the support changes your plan.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it. The README records whether you marked the handwritten pseudocode as complete; the paper itself is handed to your teacher separately.