« Back
package com.collection.programs; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class IteartorAndListIterator { public static void main(String[] args) { // TODO Auto-generated method stub List
list = new ArrayList
(); list.add(10); list.add(1); list.add(12); list.add(4); list.add(8); System.out.println(list); // Iterator traversal Iterator itr = list.iterator(); System.out.print("Iterator of List : "); while (itr.hasNext()) { System.out.print(itr.next() + " "); // we can typecast here // Integer it =(Integer)itr.next(); } System.out.println(); // listIterator Traversal ListIterator
listItr = list.listIterator(); System.out.println("move in the forward direction"); while (listItr.hasNext()) { System.out.print(listItr.next() + " "); } System.out.println(); System.out.println("move in the backward direction"); while (listItr.hasPrevious()) { System.out.print(listItr.previous() + " "); } System.out.println(); System.out.println("iterate list using for loop"); for(int i =0;i < list.size();i++) { System.out.print(list.get(i)+" "); } System.out.println(); System.out.println("iterate list using for each loop"); for(Object o:list) { System.out.print(o+" "); } } }
[10, 1, 12, 4, 8]
Iterator of List : 10 1 12 4 8
move in the forward direction
10 1 12 4 8
move in the backward direction
8 4 12 1 10
iterate list using for loop
10 1 12 4 8
iterate list using for each loop
10 1 12 4 8
×
Report a Problem:
Description:
Submit