📚 Arrays in Action: Definition, Operations & Typical Scenarios | 数组的应用:定义、操作与典型场景
Arrays are one of the most fundamental data structures in computer science, providing a contiguous block of memory to store multiple elements of the same data type under a single identifier. In the Cambridge International A-Level Computer Science syllabus, understanding how arrays are defined, manipulated, and applied in real-world algorithms is essential for both Paper 1 (theory) and Paper 2 (programming) success.
数组是计算机科学中最基础的数据结构之一,它在内存中开辟一段连续的存储区域,用同一个标识符来存放多个相同数据类型的元素。在剑桥国际 A-Level 计算机科学考纲中,理解数组如何被定义、操作以及在现实算法中的应用,对 Paper 1(理论)和 Paper 2(编程)都至关重要。
1. What Is an Array? | 什么是数组?
An array is a data structure that stores a fixed-size, ordered collection of elements. Each element is accessed by its index (or subscript), which typically starts from 0 in C-like languages, although some languages such as Pascal allow the programmer to specify a lower bound such as 1. All elements in the array must share the same data type — a design choice that simplifies memory calculation and access speed.
数组是一种存储固定大小、有序元素集合的数据结构。每个元素通过其索引(或下标)来访问,在类 C 语言中索引通常从 0 开始;而 Pascal 等语言允许程序员指定下界(例如从 1 开始)。数组中所有元素必须具有相同的数据类型——这一设计简化了内存的计算与访问速度。
In terms of memory, an array is allocated as one continuous block. If the base address is B, each element occupies S bytes, and the address of element at index i is given by:
就内存而言,数组以一段连续的内存块被分配。假设基地址为 B,每个元素占据 S 字节,那么索引 i 处元素的地址为:
Address[i] = B + i × S
This direct mapping allows O(1) access time, which is one of the main reasons arrays are chosen over linked structures when random access is frequent.
这种直接的映射关系使访问时间复杂度为 O(1),这也是在频繁随机存取时,数组往往比链表结构更受青睐的主要原因。
2. Defining Arrays: Declaration and Initialisation | 数组定义:声明与初始化
In high-level programming, an array must be declared before use, specifying the type and the number of elements. In C# (often used in CIE examinations), the syntax is:
在高级编程语言中,数组必须先声明再使用,并需指定类型和元素个数。在 C#(CIE 考试常用语言)中,语法为:
-
Declaration:
int[] scores = new int[10];— creates an array of 10 integers, each initialised to 0.声明:
int[] scores = new int[10];—— 创建一个含有 10 个整数的数组,每个元素默认初始化为 0。 -
Initialisation with values:
int[] marks = {45, 67, 89, 32, 78};— the size is inferred from the number of initialisers.带初值的初始化:
int[] marks = {45, 67, 89, 32, 78};—— 数组大小由初始化值的个数自动确定。 -
Two-dimensional array:
int[,] matrix = new int[3, 3];— a 3×3 grid, often used to represent matrices or tables.二维数组:
int[,] matrix = new int[3, 3];—— 一个 3×3 的网格,常用于表示矩阵或表格。
It is important to distinguish between a static array and a dynamic one. In A-Level contexts, the basic (static) array has a fixed length determined at compile time and cannot be resized. Dynamic arrays — such as List<T> in C# or ArrayList in Java — resize automatically, but the underlying storage is still a contiguous block that gets reallocated when full.
区分静态数组与动态数组十分重要。在 A-Level 学习情境中,基本(静态)数组的长度在编译时确定且无法调整。动态数组——如 C# 中的 List<T> 或 Java 中的 ArrayList ——可自动调整大小,但其底层存储仍是一段连续内存块,在容量满时会被重新分配。
3. Traversing: Iterating Through Elements | 遍历:迭代每一个元素
Traversal is the process of visiting each element of the array exactly once. The most common approach is a for-loop with the loop variable acting as the index. For a one-dimensional array, this is straightforward:
遍历是对数组中的每个元素恰好访问一次的过程。最常见的方法是使用 for 循环,将循环变量作为下标。对于一维数组,写法非常直接:
-
Forward traversal:
for (int i = 0; i < n; i++) { process(arr[i]); }— visits from index 0 to n−1.正向遍历:
for (int i = 0; i < n; i++) { process(arr[i]); }—— 从下标 0 访问到 n−1。 -
Reverse traversal:
for (int i = n−1; i >= 0; i−−) { process(arr[i]); }— used when order must be reversed.反向遍历:
for (int i = n−1; i >= 0; i−−) { process(arr[i]); }—— 当需要逆序处理时使用。 -
For-each loop:
foreach (int v in arr) { process(v); }— simpler but gives no index information.增强 for 循环:
foreach (int v in arr) { process(v); }—— 更简洁,但无法获取下标信息。
Traversal forms the basis of many operations: finding the sum, counting occurrences, printing elements, and searching. The time complexity of a complete traversal is O(n), where n is the size of the array.
遍历构成了许多操作的基础:求和、统计出现次数、输出元素和查找等。完整遍历的时间复杂度为 O(n),其中 n 为数组大小。
4. Searching: Linear vs Binary Search | 查找:线性查找与二分查找
Searching an array is a typical operation in programming exams. Two algorithms are especially emphasised in CIE: linear search and binary search.
查找数组是程序设计考试中的典型操作。CIE 特别强调两种算法:线性查找和二分查找。
Linear search scans the array sequentially from the first element, comparing each value to the target, until a match is found or the end of the array is reached. It works on unsorted and sorted arrays alike, and its worst-case time complexity is O(n).
线性查找从数组第一个元素开始逐个扫描,将每个值与目标值比较,直至找到匹配项或到达数组末尾。它既适用于未排序数组,也适用于已排序数组,最坏情况时间复杂度为 O(n)。
Binary search is much more efficient but requires the array to be sorted. It repeatedly divides the search interval in half:
二分查找效率高得多,但要求数组已排序。它不断将搜索区间减半:
-
Compare the target with the middle element; if equal, the search ends.
将目标值与中间元素比较;若相等,查找结束。
-
If the target is smaller, repeat the search in the left half.
若目标值较小,则在左半部分继续查找。
-
If the target is larger, repeat the search in the right half.
若目标值较大,则在右半部分继续查找。
Worst-case time: O(log₂ n)
The binary search algorithm is a classic example of the divide-and-conquer strategy, and is frequently tested in A-Level pseudocode questions.
二分查找是分治策略的经典范例,在 A-Level 伪代码题中频繁出现。
5. Inserting and Deleting Elements | 插入与删除元素
Insertion and deletion in a static array pose a special challenge because the size is fixed, and elements are stored contiguously. Shifting is required to maintain order.
在静态数组中进行插入和删除操作充满挑战,因为长度固定且元素连续存放,必须通过移动来维持顺序。
Insert at a given position: Assuming there is spare capacity, first shift all elements from the insertion point to the right by one position, then place the new value in the freed slot. This provides a worst-case shift of n elements.
在指定位置插入:假设数组尚有剩余空间,则先将插入位置及其右侧所有元素右移一位,然后将新值放入空出的位置。最坏情况下需移动 n 个元素。
Delete at a given position: Shift all elements after the deletion point one position to the left, and optionally set the last unused slot to a default value (e.g., 0) or reduce the logical length.
删除指定位置的元素:将删除位置之后的所有元素左移一位,并可选地将末尾空余位置设为默认值(如 0),或减少逻辑长度。
In a true static array, deletion does not physically shrink the array — only the logical size is reduced. The time complexity for both insertion and deletion at an arbitrary position is O(n). if performed at the end of the array, it is O(1).
在真正的静态数组中,删除并不会物理性地缩小数组——只是逻辑长度被减小。任意位置插入和删除的时间复杂度均为 O(n);如果在数组末尾进行,则为 O(1)。
6. Two-Dimensional Arrays and Matrices | 二维数组与矩阵
A two-dimensional array can be viewed as a table of rows and columns, where each element is accessed by two indices: arr[row, col] in C# or arr[row][col] in Java. In memory, it is stored row-major (as a sequence of rows) by default in most languages.
二维数组可以看作是行与列构成的表格,通过两个下标访问元素:C# 中为 arr[row, col],Java 中为 arr[row][col]。在内存中,大多数语言默认按行优先(逐行连续存放)方式存储。
Typical operations on a 2D array include traversing row by row, column by column, computing the sum of each row, transposing the matrix, and checking for symmetry:
二维数组的典型操作包括逐行遍历、逐列遍历、计算每行总和、矩阵转置以及检查对称性:
-
Matrix addition: To add two matrices of the same dimension, add corresponding elements — requires nested loops.
矩阵加法:将两个同维度矩阵的对应元素相加——需要使用嵌套循环。
-
Matrix multiplication:
C[i][j] = Σₖ A[i][k] × B[k][j], with triply nested loops.矩阵乘法:
C[i][j] = Σₖ A[i][k] × B[k][j],需三重循环实现。 -
Finding maximum in each row: iterate through each row keeping a running maximum.
求每行最大值:遍历每行并逐一更新最大值。
C[i][j] = A[i][0]×B[0][j] + A[i][1]×B[1][j] + … + A[i][k]×B[k][j]
7. Typical Scenario 1: Statistics and Data Processing | 典型场景一:统计与数据处理
In CIE practical exams, arrays are commonly used to store data entered by users, then perform aggregate operations. For example, a teacher wants to store the test scores of 30 students and calculate the average, highest and lowest scores, and the number of students who passed (score ≥ 40).
在 CIE 实践考试中,数组常被用来存储用户输入的数据,再执行汇总操作。例如,一位老师想存储 30 名学生的测验成绩,并计算平均分、最高分、最低分以及及格(≥ 40 分)的人数。
-
Store each score in an array of size 30.
将每个成绩存入大小为 30 的数组。
-
Traverse once to accumulate total and track max/min.
遍历一次以累加总分并记录最大/最小值。
-
Traverse again (or during the same loop) to count the number of scores above the passing threshold.
再次遍历(或同一个循环内)统计高于及格线的分数个数。
This scenario highlights the advantage of arrays for fixed-quantity data: a single variable name with indexed access dramatically simplifies repeated processing.
这一场景充分体现了数组处理固定数量数据的优势:单一变量名加上下标访问,极大简化了重复处理逻辑。
8. Typical Scenario 2: Lookup Tables | 典型场景二:查找表
A lookup table stores precomputed values so that a result can be retrieved directly without recalculating. For example, a program that converts a numeric grade (0–100) to a letter grade (A, B, C, D, F) can store the grade bands in an array. For a scale of 0–100, an array of 101 entries can map each possible score directly to its band.
查找表存储预先计算好的值,使结果无需重新计算即可直接获取。例如,一个将数字成绩(0–100)转换为等级(A、B、C、D、F)的程序可以用数组存储分数段。对于 0–100 的区间,可建立一个含 101 个元素的数组,将每个可能的分数直接映射到对应等级。
More abstractly, a lookup table can be used for month names:
更抽象地,查找表还可用于月份名称的存储:
| Index | 1 | 2 | 3 | … | 12 |
| Value | January | February | March | … | December |
This demonstrates mapping an index to a result in O(1) time — an extremely efficient use of array indexing.
这展示了以 O(1) 时间将下标映射到结果的用法——这是对数组下标极其高效的利用。
9. Typical Scenario 3: Sorting Applications | 典型场景三:排序应用
Sorting is one of the most important applications of arrays. For A-Level CIE Computer Science, bubble sort and insertion sort are core requirements, although the concept of sorting applies to any array-based algorithm.
排序是数组最重要的应用之一。对于 CIE A-Level 计算机科学,冒泡排序和插入排序是核心要求,但排序的概念适用于所有基于数组的算法。
-
Bubble sort: Repeatedly compares adjacent elements and swaps them if they are in the wrong order, with each pass placing the next largest (or smallest) element into its final position.
冒泡排序:反复比较相邻元素并在顺序错误时交换,每一轮将下一个最大(或最小)元素放到最终位置。
-
Insertion sort: Builds a sorted sub-array by taking one element at a time and inserting into its correct position among the already-sorted elements.
插入排序:每次取一个元素,将其插入到已排序子数组的正确位置,逐步构建有序序列。
Both algorithms operate in-place on the array, requiring no additional data structure. Their average time complexity is O(n²), while faster algorithms such as merge sort (O(n log₂ n)) also rely on auxiliary arrays for merging.
两种算法均在数组上原地操作,不需要额外数据结构。它们的平均时间复杂度为 O(n²);而更快的算法如归并排序(O(n log₂ n))仍需借助辅助数组进行归并。
10. Typical Scenario 4: Implementing Other Data Structures | 典型场景四:实现其他数据结构
Arrays are the foundation for implementing more complex data structures. Stacks and queues, for example, are often built using arrays with a pointer or index for the top or front/rear positions.
数组是实现更复杂数据结构的基础。例如,栈和队列通常利用数组加上指向栈顶或队首/队尾的指针(下标)来构建。
-
Stack using an array: A top pointer indicates the index of the last pushed element; push increments, pop decrements.
用数组实现栈:top 指针指示最后入栈元素的下标;入栈时递增,出栈时递减。
-
Queue using an array: A circular queue avoids wasted space by wrapping the front/rear pointers modulo the array size.
用数组实现队列:循环队列通过让队首/队尾指针按数组大小取模来避免空间浪费。
The ability to implement these structures demonstrates a deep understanding of array indexing and memory layout — a recurring theme in exam questions about data structures.
能够实现这些结构,体现了对数组下标与内存布局的深入理解——这也是考试中数据结构相关题目的反复主题。
11. Common Pitfalls in Excel | 常见错误与考场易错点
Several typical mistakes are observed in examination scripts when dealing with arrays:
在考卷中,处理数组时往往出现一些典型错误:
-
Off-by-one errors: Using
i <= ninstead ofi < ncauses an index out of range, especially in languages with zero-based indexing.差一错误:使用
i <= n而不是i < n会导致下标越界,尤其是在从 0 开始计数的语言中。 -
Forgetting initialisation: Using an array element before assigning a value leads to unpredictable behaviour.
忘记初始化:在赋值前使用数组元素,会导致不可预测的行为。
-
Type confusion: Mixing data types within one array is illegal in strongly typed languages like C# and Java.
类型混淆:在 C# 和 Java 等强类型语言中,在同一数组中混用不同数据类型是不合法的。
-
0-based vs 1-based confusion: In pseudocode questions, the index base may vary; always pay attention to the stated convention.
0 基与 1 基混淆:在伪代码题中,索引基数可能不同;务必留意题目给出的约定。
Careful boundary checking and clear reasoning about indices will prevent the majority of marks lost in array questions.
仔细进行边界检查并对下标做出清晰推理,可以避免数组类题目中大多数失分。
12. Summary and Exam Strategy | 总结与应试策略
Arrays are indispensable in A-Level Computer Science. A solid grasp of array definition, traversal, searching, insertion/deletion, and their real-world uses directly supports exam performance in both theory and programming sections.
数组在 A-Level 计算机科学中不可或缺。扎实掌握数组的定义、遍历、查找、插入/删除及其现实应用,能够直接提升理论与编程两个部分的考试成绩。
-
Understand index base and memory addressing formula:
Address[i] = Base + i × Size.理解索引基址与内存寻址公式:
Address[i] = Base + i × Size。 -
Practise writing pseudocode for linear search and binary search with edge-case handling.
练习编写包含边界情况处理的线性查找与二分查找伪代码。
-
Be able to trace bubble sort and insertion sort step-by-step on paper.
能够在纸上逐步追踪冒泡排序与插入排序的执行过程。
-
Practise 2D-array manipulation involving nested loops.
练习涉及嵌套循环的二维数组操作。
Mastering these skills not only secures marks but also builds a foundation for advanced topics such as abstract data types, recursion, and object-oriented collections.
掌握这些技能不仅能够稳拿分数,更将为后续进阶主题(如抽象数据类型、递归及面向对象集合)打下坚实基础。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply