Data Abstraction
How lists and strings store values in order, how a list is created and copied, and why giving a whole collection one name makes programs easier to write and change.
1Lists, elements, and indices
A list is an ordered sequence of values, written [value1, value2, value3, …]: value1 is the first element, value2 the second, and so on. Each value is an element, and each element has its own index, a whole number that says where it sits. A string works the same way with characters.
| Word | Meaning | Example from Figure 1 |
|---|---|---|
| list | an ordered sequence of elements | [12, 7, 30, 7, 25] |
| element | one value in a list; each element has its own index | the 30 |
| index | a whole number used to point to one element of a list or one character of a string | 3 |
| string | an ordered sequence of characters | "AP CS" |
Order is part of the meaning. [3, 1, 2] and [1, 2, 3] hold the same numbers but are different lists, and "CAT" and "ACT" are different strings. An index is a position, not a value: in houses, the element at index 3 is 30.
2Making a list
| Reference language | What it does |
|---|---|
aList ← [value1, value2, value3, …] | creates a new list with those values at indices 1, 2, 3, … and assigns it to aList |
aList ← [] | creates a new empty list (length 0) and assigns it to aList |
aList ← bList | assigns a copy of the list bList to aList |
The last row is the one that surprises people. Assigning a list makes a copy, so the two lists are separate afterward:
bList ← [20, 40, 60] aList ← bList bList[1] ← 5 DISPLAY(aList) DISPLAY(bList)
This displays [20, 40, 60] and then [5, 40, 60]. Changing bList after the copy did nothing to aList.
3Staying inside the list
Indices run from 1 to the length of the list. Many real programming languages start counting at 0; the reference language does not. Asking for an index below 1 or above the length is an error: the program shows an error message and stops.
scores ← [88, 92, 75] DISPLAY(scores[0])
This list has 3 elements, so the only valid indices are 1, 2, and 3. scores[0] stops the program, and so would scores[4].
4Data abstraction
Suppose a program averages five daily temperatures. Without a list, each one needs its own variable:
day1 ← 61 day2 ← 64 day3 ← 58 day4 ← 70 day5 ← 67 average ← (day1 + day2 + day3 + day4 + day5) / 5 DISPLAY(average)
temps ← [61, 64, 58, 70, 67]
total ← 0
FOR EACH t IN temps
{
total ← total + t
}
DISPLAY(total / LENGTH(temps))
Both display 64. Now add a sixth day. The first version needs a new variable and a new formula. In the list version you only add the number to the list; the loop and the average are unchanged, and the program displays 66.
That is data abstraction: giving a collection of data one name without referring to the details of how it is stored. It separates the abstract properties of the data (an ordered collection you can index and count) from the concrete details of its representation (where and how the computer keeps it).
This is how data abstraction manages complexity:
- Many related items are treated as a single value, so code can pass, copy, or loop over the whole collection by name.
- The code does not depend on how many items there are, so it works for 5 temperatures or 5,000.
- The program is easier to develop and maintain: a change in the data, like a new day, does not ripple through the code.
Lists are called different names in different languages; array is a common one. The idea is the same.
A list is an ordered sequence of elements, each with an index from 1 to the length. Using a list as a data abstraction gives a whole collection one name, hides how it is stored, and lets the same code work no matter how many items there are.
5Mixed types in one list
A data abstraction can hold different types of elements. One list can describe a student with a string, a number, and a Boolean:
student ← ["Dev", 17, true] DISPLAY(student[1]) DISPLAY(student[2] + 1)
It displays Dev and then 18. The program treats the student as one value named student, and reaches each detail by its index.
6Worked examples
Duplicates and the last element
For v ← [9, 4, 4, 1], find (a) v[2], (b) every index where 4 appears, (c) v[LENGTH(v)].
- 1(a) Count from 1: 9 is at index 1, so
v[2]is 4. - 2(b) The value 4 is at indices 2 and 3. Two elements can hold the same value; each still has its own index.
- 3(c)
LENGTH(v)is 4, andv[4]is 1. This expression always gives the last element.
The backup
What is displayed?
original ← ["red", "green", "blue"] backup ← original original[2] ← "gold" DISPLAY(backup[2])
- 1Line 2 gives
backupa copy of the list as it is now. - 2Line 3 changes element 2 of
originalonly.
green. The backup kept the old value.Why use a list?
A game keeps the high scores of its top 10 players. Explain how storing them in one list named highScores manages complexity, compared with ten variables.
- 1The list gives the ten scores one name, so the code can treat them as a single value.
- 2A loop over
highScorescan display, sort, or search all of them without naming each one. - 3If the game later keeps the top 20, the code that uses
highScoresdoes not need to change.
One past the end
What happens when this segment runs?
quiz ← [7, 9, 10] i ← LENGTH(quiz) + 1 DISPLAY(quiz[i])
- 1
LENGTH(quiz)is 3, soiis 4. - 2Valid indices are 1 to 3. Index 4 is past the end.
7Common mistakes
The first element is at index 1, and the last is at index LENGTH(aList). aList[0] is an error.
“The element at index 3” is a value in the list. “The index of 30” is a position. Read which one the question wants.
aList ← bList stores a copy. Later changes to either list do not affect the other.
In "AP CS" the space is character 3, and the string has length 5.
“The program stores the temperatures in one list named temps. This data abstraction manages complexity because the code refers to the whole collection by one name instead of a separate variable for each day, so the same loop works for any number of days and the program is easier to change.”
Name the list, say what it holds, and say how one name for the collection makes the code simpler or easier to change.