1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* Iterate through elements Java Vector using Iterator Example This Java Example shows how to iterate through the elements of java Vector object using Iterator. */ import java.util.Vector; import java.util.Iterator; public class IterateThroughVectorUsingIteratorExample { public static void main(String[] args) { //create Vector object Vector v = new Vector(); //Add elements to Vector v.add("1"); v.add("2"); v.add("3"); v.add("4"); v.add("5"); //get an Iterator object for Vector using iterator() method. Iterator itr = v.iterator(); //use hasNext() and next() methods of Iterator to iterate through the elements System.out.println("Iterating through Vector elements..."); while(itr.hasNext()) System.out.println(itr.next()); } } /* Output would be Iterating through Vector elements... 1 2 3 4 5 */ |
Add Comment