> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/programming-language/kotlin/shu-ju-lei-xing/rong-qi-lei-xing.md).

# 容器类型

## 数组

在Kotlin中，数组类型分为：

1. 原生类型数组，他在内存中是连续存储的基本类型值的集合，不包含对象头信息，因此可以更高效地存储大量的基本类型数据。主要包括：
   1. `IntArray`，对应基本类型`int`
   2. `DoubleArray`，对应基本类型`double`
   3. `FloatArray`，对应基本类型`float`
   4. `LongArray`，对应基本类型`long`
   5. `ShortArray`，对应基本类型`short`
   6. `ByteArray`，对应基本类型`byte`
   7. `BooleanArray`，对应基本类型`boolean`
2. 对象类型数组，数组元素是对象，包含对象头和实际数据。与原生类型数组不同，对象类型数组不能直接用于高性能计算：
   1. `Array`
   2. `Array<Any>`

Kotlin中的数组是不可变的，即数组创建后其大小和元素类型就不能更改。如果需要修改数组的元素，可以使用相应的数组下标运算符进行赋值操作。同时，Kotlin也提供了许多方便的函数和运算符来处理数组，例如`size`属性、`forEach`函数、`filter`函数等等。

### 创建数组

```kotlin
// 创建一个 Array<String> 初始化为 ["0", "1", "4", "9", "16"]
val asc = Array(5) { i -> (i * i).toString() }

// 创建一个原生数组，指定长度
val arr = IntArray(5)

// 创建一个原生数组，指定元素
val x: IntArray = intArrayOf(1, 2, 3)

// 创建一个原生数组，使用 lambda 表达式初始化数组中的值，it代表循环索引
var arr = IntArray(5) { it * 1 } // 数组的值为 [0, 1, 2, 3, 4]

// 创建一个原生数组，使用 lambda 表达式初始化数组中的值
val arr = IntArray(5) { 42 } // 数组的值为 [42, 42, 42, 42, 42] 
```

### 获取长度

数组的长度获取是通过访问数组的`size`属性来获得的：

```kotlin
val arr = intArrayOf(1, 2, 3)
println(arr.size) // 3
```

### 访问数组元素

数组的访问使用`[]`运算符完成：

```kotlin
val arr = intArrayOf(1, 2, 3, 4, 5)
println(arr[0]) // 输出 1
println(arr[3]) // 输出 4
arr[2] = 10
println(arr[2]) // 输出 10
```

### 数组的循环

```kotlin
val arr = arrayOf(1, 2, 3, 4, 5)

// for 循环
for (i in arr.indices) {
    println(arr[i])
}

// for in 循环
for (element in arr) {
    println(element)
}


// while循环
var i = 0
while (i < arr.size) {
    println(arr[i])
    i++
}

// forEach循环
arr.forEach { item ->
    println(item)
}

// forEachIndexed
arr.forEachIndexed { index, number ->
    println("Element $index is $number")
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/programming-language/kotlin/shu-ju-lei-xing/rong-qi-lei-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
