10-数组

Wan Yutong Lv2

03-Types中我们介绍过,Alum中的T[N]也就是C语言中的T[]类型,其本质是一个指针,指向数组第一个元素的地址。我们通过实例来说明T[]的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$import "io.al"
$import "convert.al"

// Arrays Example
// Demonstrates array operations with new T[N] syntax

fun main(): int {
// Array literal with explicit values
// Type is inferred from elements
let numbers: int[5] = [1, 2, 3, 4, 5]

// Access array elements
println("First element: ")
println(itoa(numbers[0]))

println("Third element: ")
println(itoa(numbers[2]))

// Modify array elements
numbers[0] = 10
numbers[2] = 30

println("After modification:")
println("First element: ")
println(itoa(numbers[0]))

println("Third element: ")
println(itoa(numbers[2]))

// Iterate through array using for loop
// for loop now iterates over arrays directly
println("All elements (using for loop):")
for num in numbers {
println(itoa(num))
}

// Range expression creates an array
println("\nRange 0..5:")
for i in 0..5 {
println(itoa(i))
}

return 0
}

注:示例代码来自Alum/examples/05_arrays.al

在示例代码中,我们看到了一些关于Alum中数组的用法,例如,数组变量的初始化方法如下:

1
let V: T[N] = [Vn, ...]

但这样初始化变量未免太愚蠢了,费时且费力,如果是一个连续的且递增的数组,我们可以直接使用..运算符,如,定义一个[0, 1, 2, ..., 99]的数组,可以直接通过0..100生成。

然而在实际应用中,我们并不总是会立即填满一个数组,而是预先申请内存,后续逐步填充,这时我们
可以用[T; N]语法做到,这会生成一个长度为N,类型为T的数组,因为还填充数据,所以默认被nil填充,可以通过A[N] = V修改数据,这为以后我们实现动态数组奠定了基础。

通过这几章的学习,我们已经可以使用Alum写出一个冒泡排序算法,这时一个经典的排序算法,特点是简单但低效:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$import "io.al"
$import "convert.al"

// Array Sort Example
// Demonstrates bubble sort algorithm

fun main(): int {
let numbers: int[8] = [64, 34, 25, 12, 22, 11, 90, 45]
let n: int = 8
let i: int = 0
let j: int = 0
let temp: int = 0

println("Original array:")
for i in 0..n {
print(itoa(numbers[i]))
print(" ")
}
println("")

// Bubble sort algorithm
i = 0
while i < n - 1 {
j = 0
while j < n - i - 1 {
if numbers[j] > numbers[j + 1] {
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
}
j = j + 1
}
i = i + 1
}

println("Sorted array:")
for i in 0..n {
print(itoa(numbers[i]))
print(" ")
}
println("")

return 0
}

注:示例代码来自Alum/examples/13_array_sort.al

  • Title: 10-数组
  • Author: Wan Yutong
  • Created at : 2026-03-01 12:32:41
  • Updated at : 2026-03-03 11:38:47
  • Link: https://cr0.dpdns.org/2026/03/01/10-Array/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
10-数组