Iterate Over Vector Elements in Java Last Updated : 26 May, 2021 Comments Improve Suggest changes Like Article Like Report Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is to run a for loop from start till the size of the vector.We can also iterate from n-1 to 0 to traverse in reverse order. Java // Java program to Iterate over Vector elements import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating Vector object of type String Vector<Integer> v = new Vector<Integer>(); // Adding elements to Vector object v.add(10); v.add(20); v.add(30); v.add(40); v.add(50); v.add(60); v.add(70); System.out.print("The vector V is: "); // Print the vector for (Integer i = 0; i < v.size(); i++) { System.out.print(v.get(i) + " "); } } } OutputThe vector V is: 10 20 30 40 50 60 70 Method 2: Enhanced for-loop The enhanced for loop is an advance version of for-loop.In this method, we take two parameters in our loop one is our variable which will store the value in the vector one by one and other is the name of our vector.The variable access all the values of our vector. We can not modify our vector in enhanced for-loop. Java // Java program to Iterate over Vector elements import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating Vector object of type String Vector<Integer> v = new Vector<Integer>(); // Adding elements to Vector object v.add(10); v.add(20); v.add(30); v.add(40); v.add(50); v.add(60); v.add(70); System.out.print("The vector V is: "); // Print the vector // x one by one holds all the values of our vector // till it reaches the end for (Integer x : v) { System.out.print(x + " "); } } } OutputThe vector V is: 10 20 30 40 50 60 70 Method 3: Using Iterator Iterators are a very powerful tool to use any collection.We make an iterator of the collection interface and make it point to the beginning of our vector.Run a while loop till the iterator does not reach the end.We use hasNext() to check whether next value is present or not.We print the value by next() function. Java // Java program to Iterate over Vector elements import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating Vector object of type String Vector<Integer> v = new Vector<Integer>(); // Adding elements to Vector object v.add(10); v.add(20); v.add(30); v.add(40); v.add(50); v.add(60); v.add(70); System.out.print("The vector V is: "); // Print the vector // Take a iterator pointing to begin Iterator<Integer> itr = v.iterator(); // Check until iterator has not reached end while (itr.hasNext()) { System.out.print(itr.next() + " "); } } } OutputThe vector V is: 10 20 30 40 50 60 70 Method 4: Using Enumeration Interface We can also use the enumeration interface to traverse our vector.It checks whether our vector has more elements using hasMoreElements() function.Until it reaches null, it prints the values using hasnextElement() method. Java // Java program to iterate oer vector elements import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating Vector object of type String Vector<Integer> v = new Vector<Integer>(); // Adding elements to Vector object v.add(10); v.add(20); v.add(30); v.add(40); v.add(50); v.add(60); v.add(70); System.out.print("The vector V is: "); // Print the vector // Get all the vector elements into enumaeration Enumeration<Integer> e = v.elements(); // Iterate until the last element while (e.hasMoreElements()) { System.out.print(e.nextElement() + " "); } } } OutputThe vector V is: 10 20 30 40 50 60 70 Comment More infoAdvertise with us Next Article How to Iterate the Vector Elements in the Reverse Order in Java? M mansiagrawal2103 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Vector +1 More Practice Tags : Java Similar Reads Get Enumeration over Java Vector In java, the vector is a typical dynamic array whose size can increase or decrease. While in array the size cannot be changed after declaration. We have to include file import java.util.Vector to use Vector and store values in it. Also, import java.util.Enumeration to use enumeration. Approach 1: Co 3 min read How to Iterate the Vector Elements in the Reverse Order in Java? The Vector class is found in java.util package and it implements List interface. The Vector class is included in the java collection framework from Java version 1.2. Unlike arrays, vectors can grow and shrink their size, and thus they are also called the Dynamic Array. Vectors are synchronized, ie t 3 min read Replacing Element in Java Vector To replace an element in Java Vector, set() method of java.util.Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of a Vector is zero-based. So, to replace the first element, 0 should be the index passed 2 min read Implementing Sorted Vector in Java Vector is a class that implements the List interface. It is a type of dynamic array that means the size of a vector can be grown or shrink during the execution of the program. The initial size of the vector is 10 and if we insert more than 10 elements then the size of the vector is increased by 100% 3 min read Java Program to Iterate Vector using Enumeration The Vector class implements a growable array of objects. It is available in java.util package. It implements the List interface. The Enumeration interface defines the methods by which you can traverse the elements in a collection of objects. Now in order to add elements Vector Syntax: public class V 2 min read Searching Elements in Vector Using Index in Java Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then th 4 min read Like