<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國際加速解決方案。 廣告
                ## [for-in和迭代器](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=for-in%e5%92%8c%e8%bf%ad%e4%bb%a3%e5%99%a8) 到目前為止,*for-in*語法主要用于數組,但它也適用于任何**Collection**對象。實際上在使用**ArrayList**時,已經看到了一些使用它的示例,下面是一個更通用的證明: ~~~ // collections/ForInCollections.java // All collections work with for-in import java.util.*; public class ForInCollections { public static void main(String[] args) { Collection<String> cs = new LinkedList<>(); Collections.addAll(cs, "Take the long way home".split(" ")); for(String s : cs) System.out.print("'" + s + "' "); } } /* Output: 'Take' 'the' 'long' 'way' 'home' */ ~~~ 由于**cs**是一個**Collection**,因此該代碼展示了使用*for-in*是所有**Collection**對象的特征。 這樣做的原因是 Java 5 引入了一個名為**Iterable**的接口,該接口包含一個能夠生成**Iterator**的`iterator()`方法。*for-in*使用此**Iterable**接口來遍歷序列。因此,如果創建了任何實現了**Iterable**的類,都可以將它用于*for-in*語句中: ~~~ // collections/IterableClass.java // Anything Iterable works with for-in import java.util.*; public class IterableClass implements Iterable<String> { protected String[] words = ("And that is how " + "we know the Earth to be banana-shaped." ).split(" "); @Override public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; @Override public boolean hasNext() { return index < words.length; } @Override public String next() { return words[index++]; } @Override public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { for(String s : new IterableClass()) System.out.print(s + " "); } } /* Output: And that is how we know the Earth to be banana-shaped. */ ~~~ `iterator()`返回的是實現了**Iterator**的匿名內部類的實例,該匿名內部類可以遍歷數組中的每個單詞。在主方法中,可以看到**IterableClass**確實可以用于*for-in*語句。 在 Java 5 中,許多類都是**Iterable**,主要包括所有的**Collection**類(但不包括各種**Maps**)。 例如,下面的代碼可以顯示所有的操作系統環境變量: ~~~ // collections/EnvironmentVariables.java // {VisuallyInspectOutput} import java.util.*; public class EnvironmentVariables { public static void main(String[] args) { for(Map.Entry entry: System.getenv().entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } ~~~ `System.getenv()`\[^7\]返回一個**Map**,`entrySet()`產生一個由**Map.Entry**的元素構成的**Set**,并且這個**Set**是一個**Iterable**,因此它可以用于*for-in*循環。 *for-in*語句適用于數組或其它任何**Iterable**,但這并不意味著數組肯定也是個**Iterable**,也不會發生任何自動裝箱: ~~~ // collections/ArrayIsNotIterable.java import java.util.*; public class ArrayIsNotIterable { static <T> void test(Iterable<T> ib) { for(T t : ib) System.out.print(t + " "); } public static void main(String[] args) { test(Arrays.asList(1, 2, 3)); String[] strings = { "A", "B", "C" }; // An array works in for-in, but it's not Iterable: //- test(strings); // You must explicitly convert it to an Iterable: test(Arrays.asList(strings)); } } /* Output: 1 2 3 A B C */ ~~~ 嘗試將數組作為一個**Iterable**參數傳遞會導致失敗。這說明不存在任何從數組到**Iterable**的自動轉換; 必須手工執行這種轉換。
                  <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>

                              哎呀哎呀视频在线观看