Open In App

Iterate Over Vector Elements in Java

Improve
Improve
Like Article
Like
Save
Share
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-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




// 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) + " ");
        }
    }
}


 
 

Output

The 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 + " ");
        }
    }
}


 
 

Output

The 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() + " ");
        }
    }
}


 
 

Output

The 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() + " ");
        }
    }
}


 
 

Output

The vector V is: 10 20 30 40 50 60 70 

 



Last Updated : 26 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads