Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 5/1-dimensional arrays/zh CN: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
115C - 一维数组 (原作者: Tao Yue, 状态: 未更改)
22
33假设你需要使用5000个整数,你会如何存储?你可以定义5000个变量:
44
5-```
5+```text
66aa, ab, ac, ad, ... aaa, aab, ... aba, ...
77```
88
99但这很繁琐(声明这些变量后,你需要读取每个变量)。
1010
1111一个数组包含多个存储空间,类型相同。你只需要指定数组名和下标就可以使用了:
1212
1313``` pascal
1414type
1515 typename = array [下标类型] of 数据类型;
1616```
1717
1818数据类型任意,甚至是一个数组。方括号中的“下标类型”可以是任何的顺序类型,即可以是整型,布尔型,字符类型,枚举类型,子界类型。
1919
2020``` pascal
2121type
2222 enum_type = 1..50;
2323 arraytype = array [enum_type] of integer;
2424```
2525
2626is equivalent to
2727
2828``` pascal
2929type
3030 arraytype = array [1..50] of integer;
3131```
3232
3333注:在此之前,程序员需要自己处理字符串存储问题。通过声明:
3434
3535``` pascal
3636type
3737 String = packed array [0..255] of char;
3838```
3939
4040使用某种特殊字符来表示字符串结束,大多使用空字符(0或ord(0)),packed指定了占用空间。
4141
4242在C或C++编程语言中,把200个字符存入长度为150的数组,会造成[缓冲区溢出](http://zh.wikipedia.org/zh-cn/%E7%BC%93%E5%86%B2%E5%8C%BA%E6%BA%A2%E5%87%BA)。如果缓冲区被覆盖并包含可执行代码,那么,攻击者将会设法进入你的系统。这就是Slammer蠕虫病毒猖獗的原因。试试在Pascal中会发什么。
4343
4444如果你需要在程序中存储大量数据,数据的下标可以循环读取出来,例如:
4545
4646``` pascal
4747type
4848 arraytype = array[1..50] of integer;
4949
5050var
5151 myarray : arraytype;
5252```
5353
5454使用:
5555
5656``` pascal
5757for count := 1 to 50 do
5858 read (myarray[count]);
5959```
6060
6161使用数组时,使用方括号`[ ]`括起下标。
6262
6363``` pascal
6464myarray[5] := 6;
6565```
6666
6767| | | |
6868|----|----|----|
6969| [[Basic Pascal Tutorial/Chapter 5/Subranges/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 5/Multidimensional arrays/zh CN|下一页]] |
7070
7171---
7272*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 5/1-dimensional arrays/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
7373Category: zh

Back to history