Arrays in practice
After this: Use Python lists, Java arrays and ArrayList with their costs in mind.
On this page · 5 sections · 3 checks
You will rarely write a dynamic array yourself, because Python and Java already give you one. This lesson connects what you learned to the tools you will really use.
The costs, by operation
The list in Python and the ArrayList in Java both work the way you have seen.
nums = [3, 1, 2]nums.append(5) # amortized O(1): write at the endnums.insert(0, 9) # O(n): every element shifts rightnums.pop() # O(1): remove the lastnums.pop(0) # O(n): every element shifts leftprint(nums, len(nums), nums[1])[3, 1, 2] 3 1
List<Integer> nums = new ArrayList<>(List.of(3, 1, 2));nums.add(5); // amortized O(1): write at the endnums.add(0, 9); // O(n): every element shifts rightnums.remove(nums.size() - 1); // O(1): remove the lastnums.remove(0); // O(n): every element shifts leftSystem.out.println(nums + " " + nums.size() + " " + nums.get(1));[3, 1, 2] 3 1
| Operation | Python list | Java ArrayList | Cost |
|---|---|---|---|
| Read by index | nums[i] | nums.get(i) | O(1) |
| Append | nums.append(x) | nums.add(x) | amortized O(1) |
| Insert at index | nums.insert(i, x) | nums.add(i, x) | O(n − i) |
| Remove last | nums.pop() | nums.remove(nums.size() - 1) | O(1) |
| Remove at index | nums.pop(i) | nums.remove(i) | O(n − i) |
| Find a value | x in nums | nums.contains(x) | O(n) |
Fixed or growing
In everyday code, Python uses one array-like type, the list, and it can grow. Java uses two. A plain array such as int[] never changes size and stores the values themselves. An ArrayList can grow, and it stores references to the values.
# Everyday Python uses one array-like type: the list. It is dynamic, and it stores references.row = [0] * 5 # a row of five cells, all 0row[2] = 7row.append(9) # a list grows; there is no fixed-size listprint(row, len(row))[0, 0, 7, 0, 0, 9] 6
int[] fixed = new int[5]; // fixed size, typed, values stored inlinefixed[2] = 7;// fixed[5] = 9; // would throw: a plain array never growsList<Integer> dynamic = new ArrayList<>(List.of(0, 0, 7, 0, 0));dynamic.add(9); // ArrayList grows for you (by about 1.5×)System.out.println(Arrays.toString(fixed) + " " + dynamic.size());[0, 0, 7, 0, 0] 6
The classic mistake
A common bug is deleting from a list while a loop is going through it. The loop moves forward one position at a time. Deleting a value shifts everything after it one cell to the left. So the value that moves into the empty position is never checked.
nums = [2, 4, 6, 8]for x in nums: # the loop walks by index while the list shifts under it if x % 2 == 0: nums.remove(x)print(nums)[4, 8]
List<Integer> nums = new ArrayList<>(List.of(2, 4, 6, 8));for (int i = 0; i < nums.size(); i++) { // the index advances while the list shifts under it if (nums.get(i) % 2 == 0) { nums.remove(i); }}System.out.println(nums);[4, 8]
Check yourself
Which of these listArrayList operations is O(n)?
What does this print?
nums = [3, 1, 2]nums.append(5) # amortized O(1): write at the endnums.insert(0, 9) # O(n): every element shifts rightnums.pop() # O(1): remove the lastnums.pop(0) # O(n): every element shifts leftprint(nums, len(nums), nums[1])List<Integer> nums = new ArrayList<>(List.of(3, 1, 2));nums.add(5); // amortized O(1): write at the endnums.add(0, 9); // O(n): every element shifts rightnums.remove(nums.size() - 1); // O(1): remove the lastnums.remove(0); // O(n): every element shifts leftSystem.out.println(nums + " " + nums.size() + " " + nums.get(1));You need a collection where you mostly append and read by position, and occasionally scan everything. What fits?
Connect
- Name the O(1) and O(n) operations on a Python list and a Java ArrayList.
- Tell a fixed Java array from an ArrayList, and know when each is right.
- Avoid deleting from a list while a loop is going through it.