Lesson 5.6
Update, add, remove
Part 5: Text, lists, stacks and queuesReading, about 6 min4 exercises in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Changing a list
By the end of this you will be able to replace a value in a list, add a value to the end, and remove a value.
Replacing the value at an index
scores = [70, 85, 90] scores[1] = 88 print(scores[1])
public static void main(String[] args) {
int[] scores = {70, 85, 90};
scores[1] = 88;
System.out.println(scores[1]);
}This prints 88. A list and an index on the left of the = mean "put this value at that index". The list is the same length as before: one value has been written over another, and the 85 is gone.
A list that grows
A list can grow while the program runs. In Python, append adds one value to the end of a list. In Java, an array cannot grow: its length is fixed when it is made. For a list that grows, Java has ArrayList, and its add adds one value to the end.
scores = [] scores.append(70) scores.append(85) scores.append(90) print(scores) print(len(scores))
import java.util.ArrayList;
public static void main(String[] args) {
ArrayList<Integer> scores =
new ArrayList<>();
scores.add(70);
scores.add(85);
scores.add(90);
System.out.println(scores);
System.out.println(scores.size());
}When it runs, it prints:
[70, 85, 90] 3
The list starts empty, and three values are added one at a time. Each new value goes after the ones already there.
In Java, ArrayList<Integer> means "a list of whole numbers". Between the angle brackets Java wants Integer, with a capital, and not int. A program that uses ArrayList starts with import java.util.ArrayList;.
An ArrayList is used a little differently from an array. Each row does one task both ways: read the value at index 1, replace it with 88, find the length, and add 95 at the end.
| Array | ArrayList |
|---|---|
scores[1] | scores.get(1) |
scores[1] = 88 | scores.set(1, 88) |
scores.length | scores.size() |
| no way to add a value | scores.add(95) |
Removing a value
scores = [70, 88, 90, 95] last = scores.pop() print(last) print(scores) first = scores.pop(0) print(first) print(scores)
import java.util.ArrayList;
public static void main(String[] args) {
ArrayList<Integer> scores =
new ArrayList<>();
scores.add(70);
scores.add(88);
scores.add(90);
scores.add(95);
int end = scores.size() - 1;
int last = scores.remove(end);
System.out.println(last);
System.out.println(scores);
int first = scores.remove(0);
System.out.println(first);
System.out.println(scores);
}When it runs, it prints:
95 [70, 88, 90] 70 [88, 90]
In Python, pop() removes the last value and gives it back, and pop(0) does the same for the value at index 0. In Java, remove takes the index of the value to remove, and gives that value back.
When a value is removed, every value after it moves down by one index. After the 70 has gone, 88 is at index 0.
A common mistake: working out an index, removing a value before it, and then using the old index. The value you wanted has moved down by one.
Now try it yourself on the questions below.
Exercise 5.6.1 · Trace itTrace: list changes
Complete the trace table for this Python code. append adds a value at the end. pop(1) removes the value at index 1 and gives it back. [4]
Line 1: nums = [5, 3, 8]Line 2: nums[0] = 6Line 3: nums.append(4)Line 4: x = nums.pop(1)Line 5: y = nums[1]Line 6: n = len(nums)Line 7: print(x + y + n)
Fill in one row each time a line changes x, y or n, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | x | y | n | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 |
Exercise 5.6.1 · Trace itTrace: list changes (Java)
Complete the trace table for this Java code. The code runs with ArrayList already imported. add puts a value at the end. remove(1) removes the value at index 1 and gives it back. set(0, 6) puts 6 at index 0. [4]
Line 1: ArrayList<Integer> nums =Line 2, indented 1 level: new ArrayList<>();Line 3: nums.add(5);Line 4: nums.add(3);Line 5: nums.add(8);Line 6: nums.set(0, 6);Line 7: nums.add(4);Line 8: int x = nums.remove(1);Line 9: int y = nums.get(1);Line 10: int n = nums.size();Line 11: System.out.println(x + y + n);
Fill in one row each time a line changes x, y or n, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | x | y | n | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 |
Exercise 5.6.25.6.2 · Write itSwap the ends
Write a function that swaps the first and last values of a list (an array in Java) of whole numbers, and returns the list.
- The list has between 1 and 20 values.
- After the swap, the value that was first is last, and the value that was last is first. Every other value stays at its index.
- A list of one value comes back unchanged.
Only the list your function returns is checked.
def swap_ends(values: list[int]) -> list[int]
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- swap_ends([3, 8, 5]) should return [5, 8, 3]: the ends change places and the middle value stays (rule 2)
- swap_ends([7]) should return [7]: one value (rule 3)
- swap_ends([1, 2, 6, 4]) should return [4, 2, 6, 1]: four values: the two in the middle stay where they are (rule 2)
public static int[] swapEnds(int[] values)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- swapEnds(new int[]{3, 8, 5}) should return [5, 8, 3]: the ends change places and the middle value stays (rule 2)
- swapEnds(new int[]{7}) should return [7]: one value (rule 3)
- swapEnds(new int[]{1, 2, 6, 4}) should return [4, 2, 6, 1]: four values: the two in the middle stay where they are (rule 2)
Exercise 5.6.35.6.3 · Write itCount up in a list
Write a function that returns a list of the whole numbers from 1 up to n, in order.
nis between 0 and 50.- When
nis 0, return an empty list. - In Java, return an
ArrayList<Integer>.
Only the list your function returns is checked.
def count_up(n: int) -> list[int]
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- count_up(4) should return [1, 2, 3, 4]: four numbers, starting at 1 and ending at n
- count_up(0) should return []: n is 0, so the list is empty (rule 2)
- count_up(1) should return [1]: one number, and it is n itself
public static ArrayList<Integer> countUp(int n)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- countUp(4) should return [1, 2, 3, 4]: four numbers, starting at 1 and ending at n
- countUp(0) should return []: n is 0, so the list is empty (rule 2)
- countUp(1) should return [1]: one number, and it is n itself
Exercise 5.6.45.6.4 · Write itMove the first to the end
Write a function that takes the first value out of a list of whole numbers, adds it to the end, and returns the list.
- The list has at least one value.
- Every other value moves down one index.
- A list of one value comes back unchanged.
- In Java, the list is an
ArrayList<Integer>.
Only the list your function returns is checked.
def move_first_to_end(values: list[int]) -> list[int]
Examples
Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.
- move_first_to_end([3, 8, 5]) should return [8, 5, 3]: the 3 leaves the front and joins the end (rule 2)
- move_first_to_end([7]) should return [7]: one value (rule 3)
- move_first_to_end([1, 2]) should return [2, 1]: two values change places (rule 2)
public static ArrayList<Integer> moveFirstToEnd(ArrayList<Integer> values)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.
- moveFirstToEnd([3, 8, 5]) should return [8, 5, 3]: the 3 leaves the front and joins the end (rule 2)
- moveFirstToEnd([7]) should return [7]: one value (rule 3)
- moveFirstToEnd([1, 2]) should return [2, 1]: two values change places (rule 2)