<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集合的變換,如果是HashSet,有一個drop方法。如果是TreeSet可以使用headSet、subSet、tailSet三個方法進行變換。 drop()方法,對集合進行丟棄,產生一個新的集合,從集合開始位置進行丟棄,方法接收丟棄元素的個數,方法簽名如下: ~~~ public fun <T> Iterable<T>.drop(n: Int): List<T> { ......................... } ~~~ 我們通過一個案例,來看看HashSet接口的變化操作,參考代碼: ~~~ fun main(args: Array<String>) { val set = mutableSetOf(1, 1, 2, 2, 3, 3) var list: List<Int>? = null list = set.drop(1) println("drop(1):${list}") } ~~~ 運行結果 ``` drop(1):[2, 3] Process finished with exit code 0 ``` headSet(EtoElement),對集合截取,返回一個新的集合,截取范圍[開始,toElement),方法簽名如下: ~~~ public SortedSet<E> headSet(E toElement) { return headSet(toElement, false); } ~~~ subSet(EfromElement,E toElement),對集合截取,返回一個新的集合,截取范圍[fromElement,toElement),方法簽名如下: ~~~ public SortedSet<E> subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } ~~~ tailSet(EfromElement),對集合截取,返回一個新的集合,[fromElement,結尾],方法簽名如下: ~~~ public SortedSet<E> tailSet(E fromElement) { return tailSet(fromElement, true); } ~~~ 我們通過一個案例去了解下,參考代碼: ~~~ fun main(args: Array<String>) { val treeSet = TreeSet<Int>() treeSet.add(0) treeSet.add(1) treeSet.add(3) treeSet.add(5) treeSet.add(7) treeSet.add(9) //5之前的元素,[開始,toElement] val headSet = treeSet.headSet(5) println("headSet:${headSet}")//[0, 1, 3] //3到7之間的元素,[fromElement,toElement) val subSet = treeSet.subSet(3, 7) println("subSet:${subSet}")//[3, 5] //3之后的元素,[fromElement,結尾] val tailSet = treeSet.tailSet(3) println("tailSet:${tailSet}")//[3, 5, 7, 9] } ~~~ 運行結果 ``` headSet:[0, 1, 3] subSet:[3, 5] tailSet:[3, 5, 7, 9] Process finished with exit code 0 ``` ### set集合下標越界問題 set集合定位元素的時候不是用get方法,使用的是elementAt方法、elementAtOrNull方法,使用elementAt方法可能出現數組越界問題。如果傳入索引是負數或者超出了集合的長度,也會出現下標越界異常。 我們先看,下標為負數的情況,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-e3a592c18c01eeb7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 我們再看,下標超出集合長度的情況,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-e66ad13d9f777cbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                  <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>

                              哎呀哎呀视频在线观看