Arrays and lists – index, insert, remove and traverse
Computer ScienceAlgorithms & Problem SolvingAges 15–16
Loading…
Sign in to playA list is shown as indexed cells with a pointer. Students read, write, append, insert and remove elements and watch the elements shift, the length change and out-of-bounds errors appear, then switch to a fixed-size array to contrast it with a dynamic list. A 2D array mode steps through row-major or column-major traversal with grid[r][c], a dictionary mode looks values up by key, and a references mode shows two names referring to one list. Every operation is shown in Python, Java or pseudocode.
Lesson: Arrays and lists: indexing, insertion, deletion and traversal
What it shows
An array stores elements in numbered positions, so any element can be read or changed in one step using its index. Inserting or deleting in the middle is slower, because every later element has to shift along; the move counter makes that cost visible. A dynamic list grows on demand, while a fixed-size array keeps its length and needs a count of the slots in use. The sandbox also covers 2D arrays with nested-loop traversal, dictionaries that look values up by key, and references: after b = a, both names refer to the same list.
How to use
Choose a Mode and a Notation. In List mode, type an Index and a Value, then click an operation such as nums.insert(2, 15) to watch the elements shift; try an index past the end to see the error. Switch Structure to Fixed-size array to insert and remove by hand. In 2D array mode, pick Traversal order and click Step. Try Dictionary mode and References mode too.
Parameters you can change
- Mode List / 1D array, 2D array, Dictionary (key → value), References and aliasing
- 1D structure Dynamic list (list, ArrayList), Fixed-size array (10)
- Code notation Python, Java, Pseudocode
- Starting number of elements 3–10
- Rows of the 2D array 2–6
- Columns of the 2D array 2–8
- Animation speed 1–5 steps/s
Questions to explore
- How many elements must move when a value is inserted at the start of a list of 10 elements?
- Why does nums[len(nums)] cause an error in Python while nums[-1] does not?
- After b = a, why does changing b[0] also change a, and how can you avoid it?