<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                集合可以理解為是一個容器,容器操作我們自然想到“增刪改查”,集合元素是定長的,所以沒有“增”和“刪”,我們來看看“改”和“查”。 [TOC] ### **修改元素:下標修改、set方法** 數組修改指定位置元素可以用下標修改、set方法修改。set方法的方法簽名如下: ``` publicoperator fun set(index: Int, value: T): Unit ``` 但是需要注意,數組起始位置從0開始,第一個元素的位置為0,第二個元素位置為1,以此類推。 我們演示下數組的修改,參考代碼: ~~~ fun main(args: Array<String>) { val intArr = arrayOf(1, 2, 3) println("------------下標修改元素-------------") intArr[0] = 4//下標形式 println("修改第一個元素為4:${intArr.toList()}") println("-----------set 方法修改元素------------") intArr.set(1, 5)//方法調用形式 println("修改第二個元素為5:${intArr.toList()}") } ~~~ 運行結果 ``` ------------下標修改元素------------- 修改第一個元素為4:[4, 2, 3] -----------set 方法修改元素------------ 修改第二個元素為5:[4, 5, 3] Process finished with exit code 0 ``` 針對以上代碼沒有難于理解的,其中為了打印數組方便,調用了數組的toList方法。 ### **獲取元素:下標獲取,get、elementAt、first、last、getOrNull** 數組獲取指定位置元素可以用**下標獲取、get方法獲取、elementAt方法獲取**。如果是特殊位置的獲取,比如第一個位置,最后一個位置,我們可以直接使用提供好的**first、last方法**。為了安全起見,還可以使用getOrNull去獲取元素,防止出現數組越界。 get方法、elementAt方法、first方法、last方法的方法簽名如下: **get源碼——[Array.kt](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin--array-index.html?lang=en)** ``` package kotlin public class Array<T> { public inline constructor(size: Int, init: (Int) -> T) public operator fun get(index: Int): T public operator fun set(index: Int, value: T): Unit public val size: Int public operator fun iterator(): Iterator<T> } ``` 其他方法出處自——**_Arrays.kt**,[Package kotlin.collections](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin-collections-index.html?lang=en) >[info] 注意:這里的`_Arrays.kt`和篇章末尾的**Arrays.kt**,不是同一個文件,它們在不同的包中 ``` @file:kotlin.jvm.JvmMultifileClass @file:kotlin.jvm.JvmName("ArraysKt") package kotlin.collections import kotlin.random.* .............. @kotlin.internal.InlineOnly public inline fun <T> Array<out T>.elementAt(index: Int): T { return get(index) } .............. public fun <T> Array<out T>.first(): T { if (isEmpty()) throw NoSuchElementException("Array is empty.") return this[0] } ............... public fun <T> Array<out T>.last(): T { if (isEmpty()) throw NoSuchElementException("Array is empty.") return this[lastIndex] } ............... public fun <T> Array<out T>.getOrNull(index: Int): T? { return if (index >= 0 && index <= lastIndex) get(index) else null } ............... ``` 稍微解釋下: * get:獲取指定位置元素; * elementAt:獲取指定位置元素; * first:獲取第一個位置元素; * last:獲取最后一個位置元素; * getOrNull:獲取指定位置元素,如果沒有獲取成功則返回null,免去了數組越界的麻煩,關于數組越界,后面一點也會專門給大家強調。 同樣,我們編寫一個案例去驗證下,參考代碼: ~~~ fun main(args: Array<String>) { val intArr = arrayOf(1, 2, 3) println("----------元素的獲取----------") println("通過[1]獲取,第2個元素:${intArr[1]}") println("通過get方法獲取,第2個元素:${intArr.get(1)}") println("通過getOrNull方法獲取,第2個元素:${intArr.getOrNull(1)}") println("通過getOrNul方法獲取,第4個元素:${intArr.getOrNull(3)}") println("通過elementAt方法獲取,第2個元素:${intArr.elementAt(1)}") println("----------特殊位置的元素獲取------------") println("通過first獲取,第一個元素:${intArr.first()}") println("通過last獲取,最后一個元素:${intArr.last()}") } ~~~ 運行結果 ``` ----------元素的獲取---------- 通過[1]獲取,第2個元素:2 通過get方法獲取,第2個元素:2 通過getOrNull方法獲取,第2個元素:2 通過getOrNul方法獲取,第4個元素:null 通過elementAt方法獲取,第2個元素:2 ----------特殊位置的元素獲取------------ 通過first獲取,第一個元素:1 通過last獲取,最后一個元素:3 Process finished with exit code 0 ``` ### **是否包含元素:contains方法** contains方法用于判斷集合中是否存包含某個元素,方法接收參數為元素,返回值Boolean類型,方法定義如下: 同樣出自——_Arrays.kt,[Package kotlin.collections](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin-collections-index.html?lang=en) ~~~ public operator fun <@kotlin.internal.OnlyInputTypes T> Array<out T>.contains(element: T): Boolean { return indexOf(element) >= 0 } ~~~ 下面例子,判斷集合中是否存在某個元素: ~~~ fun main(args: Array<String>) { val intArr = arrayOf(1, 2, 3) println("----------元素的判斷是否包含------------") println("intArr是否包含元素1:${intArr.contains(1)}") println("intArr是否包含元素4:${intArr.contains(4)}") } ~~~ 運行結果 ``` ----------元素的判斷是否包含------------ intArr是否包含元素1:true intArr是否包含元素4:false Process finished with exit code 0 ``` ### **獲取元素索引:indexOf方法** indexOf()獲取元素對應的位置,如果數組中不存在該元素返回-1,參考代碼: ~~~ fun main(args: Array<String>) { val intArr = arrayOf(1, 2, 3) println("----------元素索引的獲取------------") println("獲取元素1的索引:${intArr.indexOf(1)}") println("獲取元素4的索引:${intArr.indexOf(4)}") } ~~~ 運行結果 ``` ----------元素索引的獲取------------ 獲取元素1的索引:0 獲取元素4的索引:-1 Process finished with exit code 0 ``` 在定義數組時,數組中的元素可以是相同的,如果想查找數組中的一個元素的角標,首先需要指定查找的這個元素,進而判斷查找的是元素中的第1個元素的角標還是最后一個元素的角標,接下來我們通過案例來學習如何查找元素對應的角標。 (1)查找數組中指定元素的第1個角標 查找數組中指定元素中的第1個元素的角標,具體代碼如下所示 ``` fun main(args: Array<String>) { var newArr: IntArray = intArrayOf(6, 4, 3, 4, 9) //定義一個數組newArr并初始化該數組 println("第一個元素4的角標為" + newArr.indexOf(4)) //打印第一個元素4的角標 } ``` 運行結果 ``` 第一個元素4的角標為1 ``` 在上述代碼中,首先定義了一個IntArray類型的數組newArr,同時初始化該數組。在該數組中可以看到有兩個元素4,接下來查找這兩個元素4中第1個元素4的角標,在第4行中通過數組的indexOf()方法來查找,該方法中傳遞的參數4就是需要查找的元素。 除了調用數組的indexOf()方法來查找指定元素中第1個元素的角標之外,還可以通過數組的indexOfFirst()方法來查找指定元素中第1個元素的角標。稍微修改一下上述代碼后的具體代碼如下所示 ``` fun main(args: Array<String>) { var newArr: IntArray = intArrayOf(6, 4, 3, 4, 9) //定義一個數組newArr并初始化該數組 var index = newArr.indexOfFirst { //查找數組中第一個元素4對應的角標 it == 4 //將需要查找的元素賦值給it } println("第一個元素4的角標為" + index) //打印第一個元素4的角標 } ``` 運行結果 ``` 第一個元素4的角標為1 ``` 在上述代碼中,第3~5行是通過調用數組的indexOfFirst()方法來查找數組中第1個元素4對應的角標,其中第4行中的it表示需要查找的元素,因此將需要查找的元素4賦值給it。最后打印數組中查找的第1個元素4的角標。 (2)查找數組中指定元素的最后一個角標 查找數組中指定元素中的最后一個元素的角標,具體代碼如下所示。 ``` fun main(args: Array<String>) { var newArr: IntArray = intArrayOf(6, 4, 3, 4, 9) //定義一個數組newArr并初始化該數組 println("最后一個元素4的角標為" + newArr.lastIndexOf(4)) //打印最后一個元素4的角標 } ``` 運行結果 ``` 最后一個元素4的角標為3 ``` 除了調用數組的lastIndexOf()方法來查找指定元素的角標之外,還可以通過數組的indexOfLast()方法來查找指定元素的最后一個角標。 ``` fun main(args: Array<String>) { var newArr: IntArray = intArrayOf(6, 4, 3, 4, 9) //定義一個數組newArr并初始化該數組 var index = newArr.indexOfLast { //查找數組中最后一個元素4對應的角標 it == 4 //將需要查找的元素賦值給it } println("最后一個元素4的角標為" + index) //打印最后一個元素4的角標 } ``` 運行結果 ``` 最后一個元素4的角標為3 ``` ### **獲取數組長度:size屬性、count方法** 獲取長度可以通過size屬性和count方法去獲取,參考代碼: ~~~ fun main(args: Array<String>) { val intArr = arrayOf(1, 2, 3) println("----------獲取數組的長度------------") println("數組intArr的長度:${intArr.size}") println("數組intArr的長度:${intArr.count()}") println("----------反轉元素-----------------") intArr.reverse() println("反轉后的數組:${intArr.toList()}") } ~~~ 運行結果 ``` ----------獲取數組的長度------------ 數組intArr的長度:3 數組intArr的長度:3 ----------反轉元素----------------- 反轉后的數組:[3, 2, 1] Process finished with exit code 0 ``` ### **反轉元素:reverse方法** reverse()方法對數組元素反轉,第1個元素變成最后一個,最后一個元素變成第1一個元素。反轉后,原來數組元素位置都倒轉過來,參考代碼: 參考上面的代碼 下面是Arrays.kt的源碼 ``` package kotlin public class ByteArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Byte) public operator fun get(index: Int): Byte public operator fun set(index: Int, value: Byte): Unit public val size: Int public operator fun iterator(): ByteIterator } public class CharArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Char) public operator fun get(index: Int): Char public operator fun set(index: Int, value: Char): Unit public val size: Int public operator fun iterator(): CharIterator } public class ShortArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Short) public operator fun get(index: Int): Short public operator fun set(index: Int, value: Short): Unit public val size: Int public operator fun iterator(): ShortIterator } public class IntArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Int) public operator fun get(index: Int): Int public operator fun set(index: Int, value: Int): Unit public val size: Int public operator fun iterator(): IntIterator } public class LongArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Long) public operator fun get(index: Int): Long public operator fun set(index: Int, value: Long): Unit public val size: Int public operator fun iterator(): LongIterator } public class FloatArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Float) public operator fun get(index: Int): Float public operator fun set(index: Int, value: Float): Unit public val size: Int public operator fun iterator(): FloatIterator } public class DoubleArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Double) public operator fun get(index: Int): Double public operator fun set(index: Int, value: Double): Unit public val size: Int public operator fun iterator(): DoubleIterator } public class BooleanArray(size: Int) { public inline constructor(size: Int, init: (Int) -> Boolean) public operator fun get(index: Int): Boolean public operator fun set(index: Int, value: Boolean): Unit public val size: Int public operator fun iterator(): BooleanIterator } ```
                  <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>

                              哎呀哎呀视频在线观看