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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### [適配器方法慣用法](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=%e9%80%82%e9%85%8d%e5%99%a8%e6%96%b9%e6%b3%95%e6%83%af%e7%94%a8%e6%b3%95) 如果現在有一個**Iterable**類,你想要添加一種或多種在*for-in*語句中使用這個類的方法,應該怎么做呢?例如,你希望可以選擇正向還是反向遍歷一個單詞列表。如果直接繼承這個類,并覆蓋`iterator()`方法,則只能替換現有的方法,而不能實現遍歷順序的選擇。 一種解決方案是所謂*適配器方法*(Adapter Method)的慣用法。“適配器”部分來自于設計模式,因為必須要提供特定的接口來滿足*for-in*語句。如果已經有一個接口并且需要另一個接口時,則編寫適配器就可以解決這個問題。 在這里,若希望在默認的正向迭代器的基礎上,添加產生反向迭代器的能力,因此不能使用覆蓋,相反,而是添加了一個能夠生成**Iterable**對象的方法,該對象可以用于*for-in*語句。這使得我們可以提供多種使用*for-in*語句的方式: ~~~ // collections/AdapterMethodIdiom.java // The "Adapter Method" idiom uses for-in // with additional kinds of Iterables import java.util.*; class ReversibleArrayList<T> extends ArrayList<T> { ReversibleArrayList(Collection<T> c) { super(c); } public Iterable<T> reversed() { return new Iterable<T>() { public Iterator<T> iterator() { return new Iterator<T>() { int current = size() - 1; public boolean hasNext() { return current > -1; } public T next() { return get(current--); } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } }; } } public class AdapterMethodIdiom { public static void main(String[] args) { ReversibleArrayList<String> ral = new ReversibleArrayList<String>( Arrays.asList("To be or not to be".split(" "))); // Grabs the ordinary iterator via iterator(): for(String s : ral) System.out.print(s + " "); System.out.println(); // Hand it the Iterable of your choice for(String s : ral.reversed()) System.out.print(s + " "); } } /* Output: To be or not to be be to not or be To */ ~~~ 在主方法中,如果直接將**ral**對象放在*for-in*語句中,則會得到(默認的)正向迭代器。但是如果在該對象上調用`reversed()`方法,它會產生不同的行為。 通過使用這種方式,可以在**IterableClass.java**示例中添加兩種適配器方法: ~~~ // collections/MultiIterableClass.java // Adding several Adapter Methods import java.util.*; public class MultiIterableClass extends IterableClass { public Iterable<String> reversed() { return new Iterable<String>() { public Iterator<String> iterator() { return new Iterator<String>() { int current = words.length - 1; public boolean hasNext() { return current > -1; } public String next() { return words[current--]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } }; } public Iterable<String> randomized() { return new Iterable<String>() { public Iterator<String> iterator() { List<String> shuffled = new ArrayList<String>(Arrays.asList(words)); Collections.shuffle(shuffled, new Random(47)); return shuffled.iterator(); } }; } public static void main(String[] args) { MultiIterableClass mic = new MultiIterableClass(); for(String s : mic.reversed()) System.out.print(s + " "); System.out.println(); for(String s : mic.randomized()) System.out.print(s + " "); System.out.println(); for(String s : mic) System.out.print(s + " "); } } /* Output: banana-shaped. be to Earth the know we how is that And is banana-shaped. Earth that how the be And we know to And that is how we know the Earth to be banana-shaped. */ ~~~ 注意,第二個方法`random()`沒有創建它自己的**Iterator**,而是直接返回被打亂的**List**中的**Iterator**。 從輸出中可以看到,`Collections.shuffle()`方法不會影響到原始數組,而只是打亂了**shuffled**中的引用。之所以這樣,是因為`randomized()`方法用一個**ArrayList**將`Arrays.asList()`的結果包裝了起來。如果這個由`Arrays.asList()`生成的**List**被直接打亂,那么它將修改底層數組,如下所示: ~~~ // collections/ModifyingArraysAsList.java import java.util.*; public class ModifyingArraysAsList { public static void main(String[] args) { Random rand = new Random(47); Integer[] ia = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List<Integer> list1 = new ArrayList<>(Arrays.asList(ia)); System.out.println("Before shuffling: " + list1); Collections.shuffle(list1, rand); System.out.println("After shuffling: " + list1); System.out.println("array: " + Arrays.toString(ia)); List<Integer> list2 = Arrays.asList(ia); System.out.println("Before shuffling: " + list2); Collections.shuffle(list2, rand); System.out.println("After shuffling: " + list2); System.out.println("array: " + Arrays.toString(ia)); } } /* Output: Before shuffling: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After shuffling: [4, 6, 3, 1, 8, 7, 2, 5, 10, 9] array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Before shuffling: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After shuffling: [9, 1, 6, 3, 7, 2, 5, 10, 4, 8] array: [9, 1, 6, 3, 7, 2, 5, 10, 4, 8] */ ~~~ 在第一種情況下,`Arrays.asList()`的輸出被傳遞給了**ArrayList**的構造器,這將創建一個引用**ia**的元素的**ArrayList**,因此打亂這些引用不會修改該數組。但是,如果直接使用`Arrays.asList(ia)`的結果,這種打亂就會修改**ia**的順序。重要的是要注意`Arrays.asList()`生成一個**List**對象,該對象使用底層數組作為其物理實現。如果執行的操作會修改這個**List**,并且不希望修改原始數組,那么就應該在另一個集合中創建一個副本。
                  <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>

                              哎呀哎呀视频在线观看