<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                迭代器模式(Iterator):提供了一種方法順序訪問一個聚合對象中的各個元素,而又不暴露其內部的表示。 ##一、uml建模: ![](https://box.kancloud.cn/2016-02-19_56c7196ddb28e.jpg) ##二、代碼實現 ~~~ /** * 示例:迭代器模式 * */ interface Iterator { /**前移 */ public Object previous(); /**后移 */ public Object next(); /**判斷是否有下一個元素 */ public boolean hasNext(); } interface Collection { public Iterator iterator(); /**取得集合中的某個元素 */ public Object get(int i); /**取得集合大小 */ public int size(); } /** * 集合 */ class MyCollection implements Collection { private String[] strArray = { "aa", "bb", "cc", "dd" }; @Override public Iterator iterator() { return new MyIterator(this); } @Override public Object get(int i) { return strArray[i]; } @Override public int size() { return strArray.length; } } /** * 迭代器 */ class MyIterator implements Iterator { private Collection collection; private int pos = -1; public MyIterator(Collection collection) { this.collection = collection; } @Override public Object previous() { if (pos > 0) { pos--; } return collection.get(pos); } @Override public Object next() { if (pos < collection.size() - 1) { pos++; } return collection.get(pos); } @Override public boolean hasNext() { if (pos < collection.size() - 1) { return true; } return false; } } /** * 客戶端測試類 * * @author Leo */ public class Test { public static void main(String[] args) { /** * 實例化容器 */ Collection collection = new MyCollection(); /** * 創建迭代器 */ Iterator iterator = collection.iterator(); /** * 遍歷集合中的元素 */ while (iterator.hasNext()) { System.out.println(iterator.next()); } } } ~~~ ##三、應用場景 遍歷、訪問集合中的某個元素等 ##四、總結 迭代器模式就是順序訪問集合中的對象,這句話包含兩層意思:一是需要遍歷的對象,即集合對象,二是迭代器對象,用于對集合對象進行遍歷訪問。
                  <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>

                              哎呀哎呀视频在线观看