[Java learning notes] Notes Iterator
1 import java. util. arrayList; 2 import java. util. iterator; 3 4 import cn. itcast. p1.bean. person; 5 6 public class ArrayListTest {7 8 public static void main (String [] args) {9 ArrayList al = new ArrayList (); 10 11 al. add (new Person ("lisi1", 22); 12 al. add (new Person ("lisi2", 23); 13 al. add (new Person ("lisi3", 24); 14 al. add (new Person ("lisi4", 25); 15 16 Iterator it = al. iterator (); 17 18 while (it. hasNext () 19 {20 // System. out. println (Person) it. next ()). getName () + "--" + (Person) it. next ()). getAge (); // Result 21 // This method is incorrect. If the number of elements in the collection container is singular, java is thrown. util. noSuchElementException error 22 23 Person p = (Person) it. next (); 24 25 System. out. println (p. getName () + "--" + p. getAge (); // result 2 26} 27 28} 29 30}