Skip to content

3.2 Data Abstraction 数据抽象

To find specific solutions to generalizable problems, programmers represent and organize data in multiple ways.

  • 为了找到可泛化问题的具体解决方案,程序员以多种方式表示和组织数据。

核心要点 Core Points

  1. A list is an ordered sequence of elements. For example, [value1, value2, value3, ...] describes a list where value1 is the first element, value2 is the second element, value3 is the third element, and so on.

    • 列表是元素的有序序列。例如,[value1, value2, value3, ...] 描述一个列表,其中value1是第一个元素,value2是第二个元素,value3是第三个元素,以此类推。
  2. An element is an individual value in a list that is assigned a unique index.

    • 元素是列表中分配了唯一索引的单个值。
  3. An index is a common method for referencing the elements in a list or string using natural numbers.

    • 索引是使用自然数引用列表或字符串中元素的常用方法。
  4. A string is an ordered sequence of characters.

    • 字符串是字符的有序序列。
  5. Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.

    • 数据抽象提供了数据类型抽象属性与其表示的具体细节之间的分离。
  6. Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.

    • 数据抽象通过为数据集合命名而不引用表示的具体细节来管理程序中的复杂性。
  7. Data abstractions can be created using lists.

    • 数据抽象可以使用列表创建。
  8. Developing a data abstraction to implement in a program can result in a program that is easier to develop and maintain.

    • 开发要在程序中实现的数据抽象可以产生更容易开发和维护的程序。
  9. Data abstractions often contain different types of elements.

    • 数据抽象通常包含不同类型的元素。
  10. The use of lists allows multiple related items to be treated as a single value. Lists are referred to by different names, such as array, depending on the programming language.

    • 使用列表允许多个相关项目被视为单个值。列表根据编程语言的不同而有不同的名称,如array
  11. The exam reference sheet provides the notation [value1, value2, value3, ...] to create a list with specified values as its items.

    • 考试参考表提供了符号[value1, value2, value3, ...]来创建以指定值作为其项目的列表。
  12. Creating a list with initial values: aList <- [value1, value2, value3, ...] creates a new list that contains the values value1, value2, value3, and ... at indices 1, 2, 3, and ... respectively and assigns it to aList.

    • 创建带初始值的列表:aList <- [value1, value2, value3, ...] 创建一个新列表,该列表包含值value1、value2、value3等,分别位于索引1、2、3等,并将其赋给aList。
  13. Creating an empty list: aList <- [] creates a new empty list and assigns it to aList.

    • 创建空列表:aList <- [] 创建一个新的空列表并将其赋给aList。
  14. List assignment: aList <- bList assigns a copy of the list bList to the list aList. For example, if bList contains [20, 40, 60], then aList will also contain [20, 40, 60] after the assignment.

    • 列表赋值:aList <- bList 将列表bList的副本赋给列表aList。例如,如果bList包含[20, 40, 60],那么赋值后aList也将包含[20, 40, 60]。
  15. The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive.

    • 考试参考表描述了一个列表结构,其索引值从1到列表中元素的数量,包括两端。
  16. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program will terminate.

    • 对于所有列表操作,如果列表索引小于1或大于列表长度,将产生错误消息并且程序将终止。

学生活动 Student Activities

  1. Represent a list or string using a variable.

    • 使用变量表示列表或字符串。
  2. Develop data abstraction using lists to store multiple elements.

    • 使用列表开发数据抽象来存储多个元素。
  3. Explain how the use of data abstraction manages complexity in program code.

    • 解释数据抽象的使用如何管理程序代码中的复杂性。

基于 VitePress 构建的 AP CSP 学习平台