| 7 | |
| 8 | public class ListIteration { |
| 9 | public static void main(String[] args) { |
| 10 | List<Pet> pets = new PetCreator().list(8); |
| 11 | ListIterator<Pet> it = pets.listIterator(); |
| 12 | while(it.hasNext()) |
| 13 | System.out.print(it.next() + |
| 14 | ", " + it.nextIndex() + |
| 15 | ", " + it.previousIndex() + "; "); |
| 16 | System.out.println(); |
| 17 | // Backwards: |
| 18 | while(it.hasPrevious()) |
| 19 | System.out.print(it.previous().id() + " "); |
| 20 | System.out.println(); |
| 21 | System.out.println(pets); |
| 22 | it = pets.listIterator(3); |
| 23 | while(it.hasNext()) { |
| 24 | it.next(); |
| 25 | it.set(new PetCreator().get()); |
| 26 | } |
| 27 | System.out.println(pets); |
| 28 | } |
| 29 | } |
| 30 | /* Output: |
| 31 | Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug, |