跳轉到內容

A Level 計算機科學程式設計指南/陣列

來自華夏公益教科書,開放的書籍,開放的世界

宣告陣列

[編輯 | 編輯原始碼]

在虛擬碼中,所有陣列都有可宣告的上限和下限。

在 VB.NET 中,所有陣列都有可宣告的上限,但下限始終為 0。

一維陣列

[編輯 | 編輯原始碼]
語言 一般用法 示例用法
虛擬碼
DECLARE <Identifier> : ARRAY[<Lower Bound>:<Upper Bound>] OF <Data Type>
DECLARE NameList : ARRAY[1:5] OF STRING
DECLARE YearlyRainfall : ARRAY[1900:2100] OF REAL
DECLARE AgeList : ARRAY[0:40] OF INTEGER
VB.NET
Dim <Identifier>(<Upper Bound>) As <Data Type>
Dim NameList(4) As String
Dim YearlyRainfall(200) As Double
Dim AgeList(40) As Integer
Python
<Identifier> = []
NameList = []
YearlyRainfall = []
AgeList = []

二維陣列

[編輯 | 編輯原始碼]
語言 一般用法 示例用法
虛擬碼
DECLARE <Identifier> : ARRAY[<Lower Bound>:<Upper Bound>, <Lower Bound>:<Upper Bound>] OF <Data Type>
DECLARE GameArea : ARRAY[1:32, 0:16] OF STRING
DECLARE SudokuGrid : ARRAY[1:9, 1:9] OF INTEGER
DECLARE PopulationDensity : ARRAY[1:50, 20:50] OF REAL
VB.NET
Dim <Identifier>(<Upper Bound>, <Upper Bound>) As <Data Type>
Dim GameArea(31, 16) As String
Dim SudokuGrid(8) As Integer
Dim PopulationDensity(49,30) As Double
Python
<Identifier1> = []
<Identifier2> = []
<Identifier1>.append(<Identifier2>)
Game = []
GameArea = []
GameArea.append(Game)

SudokuX = []
SudokuGrid = []
SudokuGrid.append(SudokuX)

Longitude = []
PopulationDensity = []
PopulationDensity.append(Longitude)

使用陣列

[編輯 | 編輯原始碼]

一維陣列

[編輯 | 編輯原始碼]
語言 一般用法 示例用法
虛擬碼
<Identifier>[<Index>] ← <Value>
NameList[1] ← "Stephen"
YearlyRainfall[1985] ← 13.73
AgeList[39] ← 17
VB.NET
<Identifier>(<Index>) = <Value>
NameList(0) = "Stephen"
YearlyRainfall(85) = 13.73
AgeList(38) = 17
Python
<Identifier>.append(<Value>)
NameList.append("Stephen")
YearlyRainfall.append(13.73)
AgeList.append(17)

二維陣列

[編輯 | 編輯原始碼]
語言 一般用法 示例用法
虛擬碼
<Identifier>[<Index>,<Index>] ← <Value>
GameArea[16, 5] ← "Fire"
SudokuGrid[9, 3] ← 7
PopulationDensity[14, 32] ← 242.023
VB.NET
<Identifier>(<Index>,<Index>) = <Value>
GameArea(15, 5) = "Fire"
SudokuGrid(8, 2) = 7
PopulationDensity(13, 12) = 242.023
Python
<Identifier1> = []
<Identifier2> = []
<Identifier1>.append(<Identifier2>)
Game.append("Fire")
GameArea.append(Game)

SudokoX.append(7)
SudokuGrid.append(SudokuX)

Longitude.append(242.023)
PopulationDensity.append(Longitude)
華夏公益教科書