← 返回资讯屏轩教育 · 行业资讯

2D Arrays | 二维数组

2026-08-29 23:51 · 本文部分内容由AI辅助创作

📚 2D Arrays | 二维数组

Arrays are one of the fundamental data structures in computer science. In the Cambridge IGCSE syllabus, you are often required to understand how arrays store data and how they can be manipulated in programming. A two-dimensional (2D) array is like a table with rows and columns, and it is especially powerful for representing scientific data such as experimental measurements, matrices, or images.

数组是计算机科学中最基本的数据结构之一。在剑桥 IGCSE 课程中,你通常需要理解数组如何存储数据,以及如何在编程中操作它们。二维数组就像一个包含行和列的表格,它在表示科学数据(如实验测量值、矩阵或图像)时尤其强大。


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

An array is a collection of elements stored under a single name. Each element can be accessed using a numeric index. For example, a one-dimensional (1D) array of five temperatures in degrees Celsius could be named temps.

数组是将多个元素存储在同一名称下的集合。每个元素可以通过数字索引来访问。例如,一个包含五个摄氏度温度值的一维数组可以命名为 temps。

Example: temps[0] = 16, temps[1] = 17, ..., temps[4] = 20.

例如:temps[0] = 16,temps[1] = 17,……,temps[4] = 20。

In IGCSE science, you might record the temperature of water every minute. This list is easily stored as a 1D array.

在 IGCSE 科学中,你可能会每分钟记录一次水的温度。这个列表很容易存储为一维数组。


2. From One Dimension to Two | 从一维到二维

readings taken at 3 different times each day, you can store the data in a 5 × 3 2D array.

二维数组增加了一个维度。你可以把它想象成由行和列组成的表格。如果你有 5 天的温度记录,并且每天在 3 个不同时间测量,那么可以将数据存储在一个 5 × 3 的二维数组中。

Number of elements = rows × columns = 5 × 3 = 15

Each element is identified by two indices: row and column. For instance, reading[2][1] represents the reading on day 3 (row index 2) at time slot 2 (column index 1).

每个元素由两个索引确定:行和列。例如,reading[2][1] 表示第 3 天(行索引 2)第 2 个时间段(列索引 1)的读数。


3. Declaring a 2D Array | 二维数组的声明

In most IGCSE exam contexts, you do not need to write full code, but you should understand how a 2D array is created. In Python, a 2D array is often a list of lists:

在大多数 IGCSE 考试情境中,你不需要编写完整代码,但你应该理解二维数组是如何创建的。在 Python 中,二维数组通常是一个列表的列表:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

a 3 × 3 matrix of integers. In a written examination, you may be asked to describe this as 'a 2D array with 3 rows and 3 columns'.

这将创建一个 3 × 3 的整数矩阵。在笔试中,你可能会被要求将其描述为“一个具有 3 行 3 列的二维数组”。


4. Indexing and Memory Layout | 索引与内存布局

Indexing a 2D array uses two numbers: row first, then column. In languages like C and Java, indices start at 0.

二维数组的索引使用两个数字:先是行,然后是列。在 C 和 Java 等语言中,索引从 0 开始。

Column 0Column 1Column 2
Row 0[0][0][0][1][0][2]
Row 1[1][0][1][1][1][2]
Row 2[2][0][2][1][2][2]

ow 1, then row 2. The memory address formula for element [i][j] in a rows × cols array is:

在按行优先顺序存储时(许多语言采用这种方式),先存储第 0 行的所有元素,然后是第 1 行、第 2 行。对于 rows × cols 数组,元素 [i][j] 的内存地址公式为:

address = base + (i × cols + j) × element_size


5. Accessing and Modifying Elements | 访问和修改元素

To read or update an element, you use both indices. For example, to add 2 to the element in row 1, column 2:

要读取或更新某个元素,你可以使用这两个索引。例如,要将第 1 行第 2 列的元素加 2:

matrix[1][2] = matrix[1][2] + 2

This changes the value from 6 to 8 in the matrix shown earlier.

在之前展示的矩阵中,这将把值从 6 改为 8。

In an IGCSE question, you might be given a trace table and asked to show the contents of an array after each update.

在 IGCSE 题目中,你可能会得到一个跟踪表,并被要求显示每次更新后数组的内容。


6. Traversing a 2D Array | 遍历二维数组

Traversing means visiting every element in a systematic way. This is usually done using nested loops.

遍历意味着按系统方式访问每一个元素。这通常使用嵌套循环来完成。

Pseudocode:

FOR row = 0 TO rows-1
    FOR col = 0 TO cols-1
        PRINT matrix[row][col]
    ENDFOR
ENDFOR

This visits the array row by row, printing each value in order.

这会逐行访问数组,按顺序打印每个值。


7. Common Operations | 常见操作

Common operations on 2D arrays include summing all elements, finding the maximum, calculating the row averages, and searching for a value.

二维数组的常见操作包括所有元素求和、查找最大值、计算行平均值以及搜索某个值。

For a 3 × 3 matrix, the sum of all elements can be written mathematically as:

对于一个 3 × 3 矩阵,所有元素的总和可以用数学形式写为:

Sum = Σᵢ Σⱼ matrix[i][j]

where i ranges over rows and j over columns.

其中 i 遍历所有行,j 遍历所有列。

Example: Find the maximum value in the matrix [[1,2,3],[4,5,6],[7,8,9]]. The maximum is 9.

例如:在矩阵 [[1,2,3],[4,5,6],[7,8,9]] 中查找最大值,最大值为 9。


广告 / Sponsored

8. Applications in Science | 在科学中的应用

Although 2D arrays are a computer science topic, they are extremely useful in science for storing and analysing data.

虽然二维数组是计算机科学的主题,但它在科学中用于存储和分析数据非常有用。

Understanding 2D arrays helps you model real-world tables and grids in computer programs.

理解二维数组有助于你在计算机程序中模拟现实世界中的表格和网格。


9. Common Mistakes | 常见错误

Students often confuse row and column order, or forget that indices start at 0.

学生经常混淆行和列的顺序,或者忘记索引从 0 开始。

MistakeCorrection错误纠正
Using [col][row] instead of [row][col]Always use row first, column second使用 [列][行] 而不是 [行][列]始终先使用行,再使用列
Setting loop conditions to <= rowsUse < rows if index starts at 0循环条件写成 <= rows如果索引从 0 开始,应使用 < rows
Forgetting to initialise a sum variableSet sum = 0 before adding忘记初始化求和变量在累加前设置 sum = 0

In exams, trace through your array carefully to avoid off-by-one errors.

在考试中,请仔细跟踪数组,以避免差一错误。


10. Practice Questions | 练习

Q1. A 4 × 6 2D array is created. How many elements does it contain?

问题 1:创建了一个 4 × 6 的二维数组。它包含多少个元素?

A = 4 × 6 = 24

Q2. Write down the index of the element in the 5th row and 2nd column of a 2D array (indices start at 0).

问题 2:在一个二维数组中(索引从 0 开始),写出第 5 行第 2 列元素的索引。

Answer: [4][1]

Q3. A biology experiment records the number of seeds germinating in 3 trays over 4 days. Suggest how a 2D array could store the results.

问题 3:一个生物学实验记录了 3 个培养皿中 4 天内发芽的种子数量。请建议如何用二维数组存储结果。

Use a 3 × 4 array: rows = trays, columns = days.


Summary | 总结

A 2D array stores data in rows and columns. You need to be comfortable with indexing, traversing, and performing simple operations. This skill is vital for solving algorithmic problems and for handling scientific data in a structured way.

二维数组以行和列的形式存储数据。你需要熟练掌握索引、遍历以及执行简单操作。这一技能对于解决算法问题以及以结构化方式处理科学数据至关重要。

Published by TutorHao | Science Revision Series | aleveler.com

广告 / Sponsored
虫虫俱乐部戏剧表演读本 第三级 6册套装 英文原版 BUG CLUB PLAYS Year 1 Green Level…
戏剧 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao参考价 ¥130.0
广告 / Sponsored
Pearson Chemistry for the IB Diploma Programme Standard Lev…
化学 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao参考价 ¥524.0

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

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao