1 PackageCom.ietree.basic.collection.loop;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7 /**8 * List Traversal9 * Ten * @authorDylan One */ A Public classListloop { - - Public Static voidMain (string[] args) { the - //Initializes a arraylist of length 10 -list<integer> list =NewArraylist<integer>(); - for(inti = 0; I < 10; i++) { + List.add (i); - } + A //several ways to loop a list: at //Way One: -Iterator it1 =list.iterator (); - while(It1.hasnext ()) { - System.out.println (It1.next ()); - } - in //Way Two: - for(Iterator it2 =list.iterator (); It2.hasnext ();) { to System.out.println (It2.next ()); + } - the //Way Three: * for(Integer temp:list) { $ System.out.println (temp);Panax Notoginseng } - the //mode four: + for(inti = 0; I < list.size (); i++) { A System.out.println (List.get (i)); the } + } - $}
Four ways to traverse list in Java