ArrayList.listIterator()
« ArrayList
ArrayList.listIterator() traverse the list in both direction of the ArrayList
We have to add the utility class .
import java.util.ListIterator;
Examples with output
« add()
ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(51);
marks.add(52);
marks.add(53);
marks.add(54);
System.out.println(marks); // [51, 52, 53, 54]
ListIterator<Integer> my_iterator = marks.listIterator();
while(my_iterator.hasNext()){
System.out.println(my_iterator.next());
}
Output
[55, 54, 55, 54]
55
54
55
54
In above code we have used next() and hasNext(), now we will traverse in reverse direction by using previous() and hasPrevious().
ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(51);
marks.add(52);
marks.add(53);
marks.add(54);
System.out.println(marks); // [51, 52, 53, 54]
ListIterator<Integer> my_iterator = marks.listIterator();
while(my_iterator.hasNext()){
System.out.println(my_iterator.next());
}
System.out.println("Forward traversing is over. ");
while(my_iterator.hasPrevious()){
System.out.println(my_iterator.previous());
}
Output
[51, 52, 53, 54]
51
52
53
54
Forward traversing is over.
54
53
52
51
« ArrayList Tutorials
This article is written by plus2net.com team.
plus2net.com