ArrayList traversal, removal and common algorithms
Computer Science · AP CSA
Question 1 of 10
0What does this code print? ArrayList<Integer> values = new ArrayList<Integer>(); values.add(13); values.add(7); values.add(13); values.add(1, 1); values.remove(0); System.out.println(values.get(1));
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
ArrayList traversal, removal and common algorithms explained
An ArrayList in Java is a resizable list of objects. Indices start at 0. The useful methods are size(), which gives the number of items, get(i) and set(i, value) to read and change an item, add(value) to append an item, and remove(i) to delete the item at index i. Traversal means visiting every item in order, usually with an indexed for loop that runs from 0 up to size() - 1.
When you remove the item at index i, every later item moves one place down. A forward loop then moves on to index i + 1 and skips the item that has just slid into position i. The enhanced for loop cannot remove items at all and usually throws a ConcurrentModificationException. The reliable method is to traverse backwards, from size() - 1 down to 0, because removing an item never shifts an item that has not yet been checked.
Worked example
Remove every 8 from the list [8, 8, 3].
Forward loop: at index 0 the value is 8, so it is removed, leaving [8, 3]. The loop moves to index 1, which now holds 3. The second 8 has slid into index 0, which the loop has already passed, so it is never checked, so the result is [8, 3], which is wrong.
Backward loop: at index 2 the value is 3, so it is kept. At index 1 the value is 8, so it is removed, leaving [8, 3]. At index 0 the value is 8, so it is removed, leaving [3]. The result is correct.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
What is printed by this Java code? ArrayList list = new ArrayList(); list.add(26); list.add(3); list.add(17); list.add(29); list.remove(1); list.add(11); System.out.println(list.size());
Medium
What is printed by this Java code? ArrayList values = new ArrayList(); values.add(11); values.add(30); values.add(19); values.add(15); values.add(20); values.add(19); int best = values.get(0); for (int v : values) { if (v < best) { best = v; } } System.out.println(best);
Hard
What does this code print? ArrayList data = new ArrayList(); data.add(22); data.add(15); data.add(14); data.add(3); data.add(10); data.add(17); for (int i = data.size() - 1; i >= 0; i--) { if (data.get(i) < 20) { data.remove(i); } } int sum = 0; for (int v : data) { sum += v; } System.out.println(sum);