Programming Companion
Java

Programming technique

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.

Python lists store several related values under one variable name. An index identifies each position, beginning at zero, and a list can grow or shrink.

IB DP CS standards B2.2.1 and B2.2.2: Compare static and dynamic data structures, then construct programs using arrays and lists.

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 is dynamic, but it can still represent a known set of indexed positions when the program deliberately avoids adding or removing elements.

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

An ArrayList can grow and shrink. Use the generic type to state what each element stores.

Growing and shrinking a list

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()

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 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 a stable number of 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.2

Create a notebook that stores up to 10 notes in an array or ArrayList. Repeatedly display the numbered notes, ask which position to edit and replace the note at that position. Reject invalid indexes and allow the user to quit.

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.

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.