Mastering Arrays for IB AQA Computer Science | IB AQA 计算机:数组 考点精讲

📚 Mastering Arrays for IB AQA Computer Science | IB AQA 计算机:数组 考点精讲

Arrays form one of the most fundamental data structures in any programming language, and they appear heavily throughout the IB AQA Computer Science specification. Understanding how to declare, manipulate, and reason about arrays is essential for success in Paper 1 (programming and algorithms) and Paper 2 (theory of computation and data representation). This article provides a comprehensive revision guide covering all key array concepts, from basic syntax to algorithmic problem-solving.

数组是任何编程语言中最基础的数据结构之一,在 IB AQA 计算机科学考试大纲中占据重要地位。掌握数组的声明、操作和推理方法,对于通过 Paper 1(编程与算法)和 Paper 2(计算理论与数据表示)至关重要。本文提供了一份全面的复习指南,涵盖从基本语法到算法解题的所有核心数组概念。

1. What is an Array? | 什么是数组?

An array is a collection of elements of the same data type stored in contiguous memory locations. Each element can be accessed directly via an index, usually starting from 0. Arrays are static in size once declared in many languages, meaning the number of elements cannot change at runtime.

数组是一种相同数据类型元素的集合,这些元素存储在连续的内存位置上。每个元素都可以通过索引直接访问,索引通常从 0 开始。在许多编程语言中,数组一旦声明,其大小就是静态的,意味着运行时元素数量不能改变。

In IB AQA pseudocode, arrays are typically defined with a fixed size and type, for example: DECLARE scores : ARRAY[0:9] OF INTEGER. This creates an array of 10 integers, indexed from 0 to 9.

在 IB AQA 伪代码中,数组通常以固定大小和类型定义,例如:DECLARE scores : ARRAY[0:9] OF INTEGER。这将创建一个包含 10 个整数的数组,索引范围为 0 到 9。


2. Declaration and Initialization | 声明与初始化

Declaring an array means telling the compiler or interpreter the array’s name, size, and data type. Meanwhile, initialization assigns values to the elements. In pseudocode, you can declare and initialise in one step: DECLARE names : ARRAY[0:2] OF STRING ← ["Alice", "Bob", "Cara"].

声明数组意味着告诉编译器或解释器数组的名称、大小和数据类型。而初始化则是为元素赋值。在伪代码中,你可以一步完成声明和初始化:DECLARE names : ARRAY[0:2] OF STRING ← ["Alice", "Bob", "Cara"]。

If you do not initialise the array explicitly, elements take default values (e.g., 0 for integers, empty string for strings). Partial initialisation is allowed in some languages, but IB pseudocode expects clear, complete assignments.

如果没有显式初始化数组,元素将采用默认值(例如,整数为 0,字符串为空字符串)。某些编程语言允许部分初始化,但 IB 伪代码要求清晰、完整的赋值。


3. Accessing Elements via Indexing | 通过索引访问元素

Each element in an array is identified by its index, which is an integer representing its position. Accessing an element is done using square brackets: arrayName[index]. Reading or writing a value outside the declared bounds results in an index out of bounds error, a common pitfall in exams.

数组中的每个元素都由其索引(表示位置的整数)来标识。使用方括号访问元素:arrayName[index]。读取或写入超出声明边界的值会导致索引越界错误,这是考试中的常见陷阱。

For example, if arr has size 5 (indices 0–4), trying to access arr[5] is illegal. In IB pseudocode, you must always ensure the index lies within the declared range.

例如,如果 arr 大小为 5(索引 0–4),尝试访问 arr[5] 是非法的。在 IB 伪代码中,必须始终确保索引位于声明范围内。


4. Traversing Arrays with Loops | 使用循环遍历数组

Traversal means visiting each element of the array, usually to process or modify its value. A FOR loop is the most common method: FOR i ← 0 TO 9 OUTPUT scores[i] NEXT i. You can also use WHILE or REPEAT...UNTIL loops when the termination condition is based on a value rather than a fixed length.

遍历是指访问数组的每个元素,通常是为了处理或修改其值。FOR 循环是最常用的方法:FOR i ← 0 TO 9 OUTPUT scores[i] NEXT i。当终止条件基于某个值而非固定长度时,也可以使用 WHILE 或 REPEAT...UNTIL 循环。

Traversal is fundamental for operations like summing elements, finding maximum/minimum, and searching. Always be careful with loop boundaries to avoid off-by-one errors.

遍历是求和、查找最大值/最小值和搜索等操作的基础。务必小心循环边界,以避免差一错误。


5. Basic Array Operations: Insert, Delete, Search | 基本数组操作:插入、删除、搜索

Because arrays are static, insertion and deletion of elements are not straightforward. To insert a new value at a specific index, you must shift all subsequent elements to the right, assuming there is free space. Similarly, deletion requires shifting elements left to fill the gap.

由于数组是静态的,插入和删除元素并不简单。要在特定索引处插入新值,必须将所有后续元素向右移动,前提是存在空闲空间。同理,删除时需要将元素向左移动以填补空缺。

Searching can be performed using linear search (iterating through each element until the target is found) or, if the array is sorted, binary search (repeatedly dividing the search interval in half). Both algorithms are explicitly tested in IB AQA.

搜索可以使用线性搜索(遍历每个元素直到找到目标)完成,如果数组已排序,也可以使用二分搜索(反复将搜索区间一分为二)。这两种算法都在 IB AQA 中明确考查。


6. Multidimensional Arrays | 多维数组

A multidimensional array stores data in a grid-like structure, such as a table with rows and columns. A 2D array is declared as ARRAY[0:2,0:3] OF INTEGER, giving 3 rows and 4 columns. Access uses two indices: grid[row][col] or grid[row,col] in pseudocode.

多维数组以网格状结构存储数据,例如包含行和列的表格。二维数组的声明为 ARRAY[0:2,0:3] OF INTEGER,产生 3 行 4 列。访问时使用两个索引:伪代码中为 grid[row][col] 或 grid[row,col]。

Applications include representing game boards, spreadsheets, and matrices. Traversing a 2D array usually requires nested loops—one for rows and one for columns. Be mindful of row-major vs. column-major order, though IB pseudocode is generally row-major.

应用包括表示游戏棋盘、电子表格和矩阵。遍历二维数组通常需要嵌套循环——一个用于行,一个用于列。注意行优先与列优先的顺序,不过 IB 伪代码一般遵循行优先。


7. Memory Representation of Arrays | 数组的内存表示

In memory, an array occupies a single contiguous block. The address of the first element is the base address. The address of any element can be calculated as: address = base_address + index * size_of_element. This constant-time calculation enables direct (random) access.

在内存中,数组占据一个连续的区块。第一个元素的地址是基地址。任意元素的地址可通过公式计算:地址 = 基地址 + 索引 × 元素大小。这种常量时间的计算实现了直接(随机)访问。

Understanding this helps explain why arrays have O(1) access time but O(n) insertion/deletion. It also clarifies why bounds must be strictly respected—accessing an out-of-bounds index could corrupt adjacent memory.

理解这一点有助于解释为什么数组的访问时间复杂度为 O(1),而插入/删除为 O(n)。这也说明了为什么必须严格遵守边界——访问越界索引可能会破坏相邻内存。


8. Arrays vs. Lists and Linked Structures | 数组与列表、链式结构的对比

In high-level languages, a list is often a dynamic data structure built on top of arrays (e.g., Python’s list, Java’s ArrayList). Unlike raw arrays, lists can grow and shrink automatically. However, their underlying implementation still uses arrays, so they may occasionally require costly resizing operations.

在高级语言中,列表通常是在数组之上构建的动态数据结构(例如 Python 的列表、Java 的 ArrayList)。与原始数组不同,列表可以自动增长和缩小。然而,其底层实现仍使用数组,因此偶尔会需要代价高昂的调整大小操作。

For IB AQA, you should distinguish between static arrays, dynamic lists, and linked lists. Linked lists offer efficient insertion/deletion but lack direct access by index. The exam often asks you to compare these structures.

对于 IB AQA,你应区分静态数组、动态列表和链表。链表提供高效的插入/删除,但缺乏通过索引进行直接访问的能力。考试经常要求比较这些结构。


9. Common Algorithms: Searching | 常见算法:查找

Linear Search checks each element sequentially until a match is found or the end is reached. Its average and worst-case time complexity is O(n). Pseudocode:

线性搜索顺序检查每个元素,直到找到匹配项或到达末尾。其平均和最坏情况时间复杂度为 O(n)。伪代码:

FOR i ← 0 TO length-1
IF arr[i] = target THEN
RETURN i
ENDIF
NEXT i
RETURN -1

Binary Search requires a sorted array. It repeatedly divides the search space in half, achieving O(log n) time. However, note that sorting is a pre-requisite, which itself takes at least O(n log n).

二分搜索需要已排序的数组。它反复将搜索空间一分为二,实现 O(log n) 的时间复杂度。不过请注意,排序是先决条件,其自身至少需要 O(n log n) 的时间。


10. Common Algorithms: Sorting | 常见算法:排序

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest unsorted element ‘bubbles up’ to its correct position. Time complexity O(n²) in the worst and average cases, O(n) in the best case if an early exit flag is used.

冒泡排序重复比较相邻元素,如果顺序错误则交换它们。每一轮遍历后,最大的未排序元素会“冒泡”到正确位置。最坏和平均情况时间复杂度为 O(n²),如果使用提前退出标志,最佳情况为 O(n)。

Selection Sort finds the minimum element from the unsorted part and swaps it with the first unsorted element. It always performs O(n²) comparisons, but swaps are only O(n). IB candidates must be able to trace and code both algorithms.

选择排序从无序部分找出最小元素,并将其与第一个无序元素交换。它始终执行 O(n²) 次比较,但交换只有 O(n) 次。IB 考生必须能够追踪并编写这两种算法。


11. Array-Based Problem Solving in the IB Exam | IB 考试中的基于数组的解题

Typical IB exam problems involve writing pseudocode to count occurrences, find extremes, accumulate sums, reverse an array, or merge two sorted arrays. You may also be asked to work with parallel arrays, where multiple arrays of the same length store related attributes (e.g., names and scores).

典型的 IB 考试问题包括编写伪代码来计算出现次数、寻找极值、累计求和、反转数组或合并两个已排序数组。你可能还会遇到并行数组的问题,即多个长度相同的数组存储相关属性(例如 names 和 scores)。

A systematic approach is key: clearly define input, output, and necessary variables; handle edge cases (empty array, single element); and ensure loop indices stay within bounds. Practice with past papers is the best preparation.

系统的方法至关重要:明确定义输入、输出和必要的变量;处理边缘情况(空数组、单个元素);并确保循环索引保持在边界内。通过历年真题练习是最好的准备方式。


12. Common Mistakes and Exam Tips | 常见错误与考试技巧

Off-by-one errors: Confusing array length with the maximum index. Remember, an array of size n has indices from 0 to n-1. Always double-check loop conditions.

差一错误: 将数组长度与最大索引混淆。请记住,大小为 n 的数组索引范围是 0 到 n–1。务必仔细检查循环条件。

Uninitialised values: Using an array without assigning values can lead to unpredictable output. In pseudocode, always initialise clearly.

未初始化值: 使用未赋值的数组可能导致输出不可预测。在伪代码中,务必清晰地初始化。

Inefficient algorithms: When asked to sort or search, choose the algorithm appropriate for the situation. If the array is sorted, binary search is better; if it is unsorted and you only search once, linear search suffices. Explaining time complexity earns marks.

低效算法: 当要求排序或搜索时,根据情况选择合适的算法。如果数组已排序,二分搜索更好;如果数组未排序且只搜索一次,线性搜索就足够了。解释时间复杂度可以得分。

Finally, practise writing clean, indented pseudocode. Examiners reward clarity. Use meaningful variable names and add brief comments where helpful.

最后,练习编写整洁、缩进正确的伪代码。考官青睐清晰的表达。使用有意义的变量名,并在有帮助的地方添加简短注释。

Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version