Code Companion
Java

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.

IB DP CS standards B2.2.1 and B2.2.2: Compare static and dynamic data structures, including memory allocation/resizing, speed, memory use and flexibility, then construct programs using 1D/2D arrays or lists and dynamic list operations.

Static versus dynamic: what actually changes?

QuestionFixed arrayDynamic ArrayList
CapacityLength is fixed when the array is created.Logical size can grow and shrink.
Resizing mechanismTo 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/predictabilitySimple fixed-capacity indexed storage; no automatic resizing step.Convenient growth, but an occasional resize can cost extra work.
MemoryUnused fixed positions may waste capacity if the estimate is too large.May keep spare capacity and has management overhead to support growth.
FlexibilityBest when the required capacity is known and stable.Best when the number of elements genuinely changes.
QuestionFixed-capacity use of a listDynamic list use
Capacity behaviourThe 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 structureA Python list is still a dynamic structure internally; “fixed-capacity use” describes the program's rule, not a separate static Python array type.
Resizing mechanismNo 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.
MemoryReserved placeholder positions can remain unused.Dynamic growth provides flexibility but may keep spare capacity and management overhead.
FlexibilityUseful when position meanings and capacity must stay stable.Useful when the number of stored elements changes during execution.
Do not over-generalise: “dynamic is better” and “static is faster” are not complete conclusions. Compare the actual scenario: how often size changes, how important predictable capacity is, and whether unused/spare storage matters.

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.

Values and their indexes
Monindex 0
Tueindex 1
Wedindex 2
Thuindex 3
Friindex 4

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.

FeatureArrayArrayList
SizeFixed after creationCan grow and shrink
Lengtharray.lengthlist.size()
Readarray[index]list.get(index)
Replacearray[index] = valuelist.set(index, value)
Add/removeNot directlyadd() and remove()
FeatureFixed-capacity useDynamic list use
SizeProgram keeps the original number of positionsProgram may grow or shrink the list
Lengthlen(values)len(values)
Readvalues[index]values[index]
Replacevalues[index] = valuevalues[index] = value
Add/removeAvoided by designappend(), insert(), pop(), remove()

Check your understanding

Answer each question before opening the model answer.

  1. 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.

  2. 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.

  3. 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.

  4. 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

Challenge ID: PC-T10-C01 · Standards: B2.2.2

Store 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

Challenge ID: PC-T10-C02 · Standards: B2.2.1, B2.2.2

Create 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.

A numbered digital notebook with one note being edited.

Currency Converter

Challenge ID: PC-T10-C03 · Standards: B2.2.2

Store 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.

Scaffold available

One-Dimensional Battleships

Challenge ID: PC-T10-C04 · Standards: B2.2.2, B2.3.2, B2.3.3

Create 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.

Scaffold available
A one-dimensional Battleships board with 50 positions, five ships, hits and misses.