OnJava8-Examples/collections/ListIteration.java

38 lines
1.1 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collections/ListIteration.java
2020-10-07 13:35:40 -06:00
// (c)2020 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import typeinfo.pets.*;
import java.util.*;
public class ListIteration {
public static void main(String[] args) {
2015-12-15 11:47:04 -08:00
List<Pet> pets = Pets.list(8);
2015-06-15 17:47:35 -07:00
ListIterator<Pet> it = pets.listIterator();
while(it.hasNext())
System.out.print(it.next() +
", " + it.nextIndex() +
2015-06-15 17:47:35 -07:00
", " + it.previousIndex() + "; ");
System.out.println();
// Backwards:
while(it.hasPrevious())
System.out.print(it.previous().id() + " ");
System.out.println();
System.out.println(pets);
it = pets.listIterator(3);
while(it.hasNext()) {
it.next();
2015-12-15 11:47:04 -08:00
it.set(Pets.get());
2015-06-15 17:47:35 -07:00
}
System.out.println(pets);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug,
5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7;
2015-06-15 17:47:35 -07:00
7 6 5 4 3 2 1 0
[Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx]
[Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster,
EgyptianMau]
2015-09-07 11:44:36 -06:00
*/