Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 2
Iterator | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 17
Iterator
Iteratorは「繰り返し」という意味で、forやwhileと併用してコレクションの要素に繰り返しアクセスすることが出来ます。

forを使用して繰り返し
import java.util.HashMap;
import java.util.Iterator;

public class ExIterator {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("key1","test1");
        map.put("key2","test2");
        map.put("key3","test3");
        
        StringBuffer buf = new StringBuffer();
        
        for (Iterator itr = map.keySet().iterator();itr.hasNext();){
            String key   = (String)itr.next();
            String value = (String)map.get(key);
            System.out.println(key +" = " + value);
        }
    }
}
whileを使用して繰り返し
import java.util.HashMap;
import java.util.Iterator;

public class ExIterator {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("key1","test1");
        map.put("key2","test2");
        map.put("key3","test3");
        
        StringBuffer buf = new StringBuffer();
        
        Iterator itr = map.keySet().iterator(); 
        while (itr.hasNext()) {
            String key   = (String)itr.next();
            String value = (String)map.get(key);
            System.out.println(key +" = " + value);
        }
    }
}
Java1.5(Java5.0)から拡張for文が新機能で使えるようになっています。

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 119

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 119

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 128

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/iterator/iterator/iterator.html on line 128