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-loop
- Enhanced for-loop
- Iterators
- Enumeration 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
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: " );
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
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: " );
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
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: " );
Iterator<Integer> itr = v.iterator();
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
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: " );
Enumeration<Integer> e = v.elements();
while (e.hasMoreElements())
{
System.out.print(e.nextElement() + " " );
}
}
}
|
OutputThe vector V is: 10 20 30 40 50 60 70