<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 集合寫操作 [TOC] [可變集合](collections-overview.html#集合類型)支持更改集合內容的操作,例如添加或刪除元素。 在此頁面上,我們將描述實現 `MutableCollection` 的所有寫操作。 有關 `List` 與 `Map` 可用的更多特定操作,請分別參見 [List 相關操作](list-operations.html)與 [Map 相關操作](map-operations.html)。 ## 添加元素 要將單個元素添加到列表或集合,請使用 [`add()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html) 函數。指定的對象將添加到集合的末尾。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf(1, 2, 3, 4) numbers.add(5) println(numbers) //sampleEnd } ``` [`addAll()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/add-all.html) 將參數對象的每個元素添加到列表或集合中。參數可以是 `Iterable`、`Sequence` 或 `Array`。 接收者的類型和參數可能不同,例如,你可以將所有內容從 `Set` 添加到 `List`。 當在列表上調用時,`addAll()` 會按照在參數中出現的順序添加各個新元素。你也可以調用 `addAll()` 時指定一個元素位置作為第一參數。參數集合的第一個元素會被插入到這個位置。其他元素將跟隨在它后面,將接收者元素移到末尾。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf(1, 2, 5, 6) numbers.addAll(arrayOf(7, 8)) println(numbers) numbers.addAll(2, setOf(3, 4)) println(numbers) //sampleEnd } ``` 你還可以使用 [`plus` 運算符](collection-plus-minus.html) - [`plusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html) (`+=`) 添加元素。當應用于可變集合時,`+=` 將第二個操作數(一個元素或另一個集合)追加到集合的末尾。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "two") numbers += "three" println(numbers) numbers += listOf("four", "five") println(numbers) //sampleEnd } ``` ## 刪除元素 若要從可變集合中移除元素,請使用 [`remove()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html) 函數。`remove()` 接受元素值,并刪除該值的一個匹配項。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf(1, 2, 3, 4, 3) numbers.remove(3) // 刪除了第一個 `3` println(numbers) numbers.remove(5) // 什么都沒刪除 println(numbers) //sampleEnd } ``` 要一次刪除多個元素,有以下函數: * [`removeAll()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove-all.html) 移除參數集合中存在的所有元素。或者,你可以用謂詞作為參數來調用它;在這種情況下,函數移除謂詞產生 `true` 的所有元素。 * [`retainAll()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/retain-all.html) 與 `removeAll()` 相反:它移除除參數集合中的元素之外的所有元素。當與謂詞一起使用時,它只留下與之匹配的元素。 * [`clear()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/clear.html) 從列表中移除所有元素并將其置空。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf(1, 2, 3, 4) println(numbers) numbers.retainAll { it >= 3 } println(numbers) numbers.clear() println(numbers) val numbersSet = mutableSetOf("one", "two", "three", "four") numbersSet.removeAll(setOf("one", "two")) println(numbersSet) //sampleEnd } ``` 從集合中移除元素的另一種方法是使用 [`minusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html) (`-=`) ——原地修改版的 [`minus`](collection-plus-minus.html) 操作符。 [`minus`](collection-plus-minus.html) 操作符。第二個參數可以是元素類型的單個實例或另一個集合。右邊是單個元素時,`-=` 會移除它的第一個匹配項。反過來,如果它是一個集合,那么它的所有元素的每次出現都會刪除。例如,如果列表包含重復的元素,它們將被同時刪除。第二個操作數可以包含集合中不存在的元素。這些元素不會影響操作的執行。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "two", "three", "three", "four") numbers -= "three" println(numbers) numbers -= listOf("four", "five") //numbers -= listOf("four") // 與上述相同 println(numbers) //sampleEnd } ``` ## 更新元素 list 和 map 還提供更新元素的操作。它們在 [List 相關操作](list-operations.html)與 [Map 相關操作](map-operations.html)中有所描述。對于 set 來說,更新沒有意義,因為它實際上是移除一個元素并添加另一個元素。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看