3.2 Data Abstraction (list)

6 阅读1分钟

1. Exam Points

  • Benefits of using lists.
    • Lists often allow their size to be easily updated to hold as many data values as needed. (append, insert, remove etc.)
    • Easier to apply the same computation to every element.
  • Index of list elements start from 1, ends at size - 1.
  • Indexex of String characters start from 1. (Ex. ”abc”, the index of a is 1)
  • Predict the result after some operations on a list.

2. Knowledge Points

(1) list (列表)

  • A list is an ordered sequence of elements(元素).
  • Format: [value1, value2, value3, ...]
  • Example:
    • [10, 20, 30, 40, 50, 60]
  • An element is an individual value in a list that is assigned a unique index.
  • The valid index of a list is of range: 1 to size - 1
  • Here value1 is the first element, value2 is the second element, value3 is the third element, and so on.
  • An index is a common method for referencing the elements in a list or string using natural numbers (1,2,3,…).
  • Example:
    • [10, 20, 30, 40, 50, 60] : index of 10 is 1, index of 60 is 6.
    • Annabelle: index of A is 1, index of b is 5.
  • Data abstractions reduce complexity in programs by giving a collection of data a name without referencing the specific details of the representation.
  • Data abstractions can be created using lists.

(2) Create a list

  • Syntax:
    image.png image.png
  • Example:
    • Create a list: aList ← [10, 20, 30, 40]
    • Create an empty list: bList ← []
    • Assign a copy of aList to bList: bList ← aList

3. Exercises