Arrays and file handling · 数组与文件处理
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| array/əˈreɪ/ | 数组 | shù zǔ |
| index/ˈɪndeks/ | 索引 | suǒ yǐn |
| nested/ˈnestɪd/ | 嵌套 | qiàn tào |
| file handling/faɪl ˈhændlɪŋ/ | 文件处理 | wén jiàn chǔ lǐ |
Arrays 数组 and files
- An array stores many values of the same type in one variable.
- A file lets a program keep data after it stops.
- Loops are the natural way to work through both.
数组与文件
- 一个数组(array)在一个变量中存储许多相同类型的值。
- 一个文件(file)让一个程序在停止后保留数据。
- 循环是遍历两者的自然方式。
One-dimensional arrays
- A 1D array is like a single list; each value is found by an index 索引 (a position number).
- A loop fills or reads the whole array.
A 1D array holds many values in one variable; each is found by its index.
一维数组
- 一个一维数组(1D array)像一个单一列表;每个值由一个索引(index,一个位置编号)找到。
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- 一个循环填充或读取整个数组。

一个一维数组在一个变量中持有许多值;每个由它的索引找到。
An array is: · 一个数组是:
An array stores many same-type values under one name; each is accessed by its index. · 一个数组在一个名称下存储许多相同类型的值;每个由它的索引访问。
Two-dimensional arrays
- A 2D array is like a table with rows and columns, using two indexes
[row, column].
- You read a 2D array with a loop inside a loop (nested 嵌套 iteration).
A 2D array is a table; each value is found by two indexes, (row, column).
二维数组
- 一个二维数组(2D array)像一个有行和列的表,使用两个索引
[row, column]。
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- 你用一个循环里面的循环(嵌套迭代)读取一个二维数组。

一个二维数组是一个表;每个值由两个索引(行、列)找到。
Index a 2-D array: Grid[Row, Col] · 用 Grid[Row, Col] 索引一个二维数组
A 2-D array is a grid of values. Grid[Row, Col] reaches exactly one cell — the row first, then the column. Cambridge counts both from 1. · 一个二维数组是一个值的网格。Grid[Row, Col] 恰好到达一个单元格——先行,后列。剑桥的行和列都从 1 开始数。
In grid[2, 3], which cell is accessed? · 在 grid[2, 3] 中,访问哪个单元格?
The first index is the row, the second the column — row 2, column 3. · 第一个索引是行,第二个是列——第 2 行,第 3 列。
Match each idea to what it means. · 把每个概念匹配到它的含义。
Arrays hold many values by index; a 2-D array is a grid; nested loops walk it; files are opened, used and closed. · 数组按索引持有许多值;一个二维数组是一个网格;嵌套循环遍历它;文件被打开、使用和关闭。
File handling 文件处理
- A program can store data in a file so it survives after the program stops.
- Open the file, use it, then close it:
- Always close a file when finished (use
FOR READto read it back).
A counter counts up by 1; a total builds a running sum
文件处理
- 一个程序能把数据存储在一个文件中,使它在程序停止后存活。
- 打开文件,使用它,然后关闭它:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- 完成时总是关闭一个文件(用
FOR READ把它读回来)。
Put the steps of using a file in the correct order. · 把使用一个文件的步骤按正确的顺序排列。
Always open first, then do the reading/writing, then close to save buffered data and free the file. · 总是先打开,然后做读取/写入,然后关闭以保存缓冲的数据并释放文件。
You should always close a file when you have finished using it. · 你应该总是在使用完一个文件后关闭它。
Closing saves any buffered data and frees the file for other programs. · 关闭保存任何缓冲的数据并为其他程序释放文件。
You've got it
- a 1D array is a list reached by one index; fill/read it with a loop
- a 2D array is a table reached by
[row, column]; use nested loops - file handling: OPENFILE (READ/WRITE) → READFILE/WRITEFILE → CLOSEFILE
- always close a file when done
你掌握了
- 一个一维数组是由一个索引到达的列表;用一个循环填充/读取它
- 一个二维数组是由
[row, column]到达的表;使用嵌套循环 - 文件处理:OPENFILE(READ/WRITE)→ READFILE/WRITEFILE → CLOSEFILE
- 完成时总是关闭一个文件