# 数组（Array）

URL: https://caijiao.org/swift/05-collections/01-array
Source: docs/swift/05-collections/01-array.md
Description: 本章介绍 Swift 中数组（Array）的定义与使用方法，帮助开发者管理有序数据集合，进行增删改查操作。

数组是存储有序同类型元素的集合，Swift 中使用 `Array` 类型表示。

## 创建数组

```swift
var numbers: [Int] = [1, 2, 3, 4, 5]
var fruits = ["苹果", "香蕉", "橘子"]  // 类型推断为 [String]
````

## 访问元素

通过索引访问数组元素，索引从 0 开始：

```swift
print(fruits[0])  // 输出：苹果
```

## 修改数组

* 添加元素：

```swift
fruits.append("葡萄")
```

* 插入元素：

```swift
fruits.insert("梨", at: 1)
```

* 删除元素：

```swift
fruits.remove(at: 2)
```

## 遍历数组

```swift
for fruit in fruits {
    print(fruit)
}
```

数组是管理有序数据的重要结构，灵活操作数组有助于编写高效程序。
