Vector with Looping

In this article, we will see how to loop Vector in java

Its very much common requirement to iterate or loop through Vector in java applications

There are mainly 5 ways to loop through Vector in java


1) Traditional For loop
2) Enhanced For loop
3) While loop
4) Iterator
5) Enumeration

Let’s see each of these ways with an example

1) Traditional For loop
  1. import java.util.*;
  2.  
  3. public class VectorTraditionalForLoop {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.         vector.add(14);
  7.         vector.add(7);
  8.         vector.add(21);
  9.         vector.add(28);
  10.     System.out.println("Traditional For Loop");
  11.         for (int index = 0; index < vector.size(); index++) {            
  12.           System.out.println(vector.get(index));       
  13.           }  
  14.      }
  15. }
import java.util.*;

public class VectorTraditionalForLoop {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
        vector.add(14);
        vector.add(7);
        vector.add(21);
        vector.add(28);
	System.out.println("Traditional For Loop");
        for (int index = 0; index < vector.size(); index++) { 		      
          System.out.println(vector.get(index)); 		
          }  
     }
}
Note : In the above example, we have used Traditional for loop to iterate the Vector

2) Enhanced For loop
  1. import java.util.*;
  2.  
  3. public class VectorEnhancedForLoop {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.         vector.add(14);
  7.         vector.add(7);
  8.         vector.add(21);
  9.         vector.add(28);
  10.     System.out.println("Enhanced For Loop");
  11.         for (Integer element : vector) {             
  12.            System.out.println(element);        
  13.          }
  14.     }
  15. }
import java.util.*;

public class VectorEnhancedForLoop {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
        vector.add(14);
        vector.add(7);
        vector.add(21);
        vector.add(28);
	System.out.println("Enhanced For Loop");
        for (Integer element : vector) { 		      
           System.out.println(element); 		
         }
    }
}
Note : In the above example, we have used advanced/enhanced for loop to iterate Vector

3) While loop
  1. import java.util.Vector;
  2.  
  3. public class VectorWhileLoop {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.       vector.add(14);
  7.       vector.add(7);
  8.       vector.add(21);
  9.       vector.add(28);
  10.       System.out.println("While Loop");        
  11.       int count = 0;       
  12.       while ( count < vector.size()) {
  13.      System.out.println(vector.get(count));
  14.          count++;
  15.       }
  16.     }
  17. }
import java.util.Vector;

public class VectorWhileLoop {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
      vector.add(14);
      vector.add(7);
      vector.add(21);
      vector.add(28);
	  System.out.println("While Loop"); 		
      int count = 0; 		
      while ( count < vector.size()) {
	 System.out.println(vector.get(count));
         count++;
      }
	}
}
Note : in the above example, we are using while loop to iterate Vector

4) Iterator
  1. import java.util.*;
  2.  
  3. public class VectorIterator {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.         vector.add(14);
  7.         vector.add(7);
  8.         vector.add(21);
  9.         vector.add(28);
  10.     System.out.println("While with iterator");
  11.         Iterator itr = vector.iterator();
  12.         while (itr.hasNext()) {
  13.           System.out.println(itr.next());
  14.         }
  15.     }
  16. }
import java.util.*;

public class VectorIterator {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
        vector.add(14);
        vector.add(7);
        vector.add(21);
        vector.add(28);
	System.out.println("While with iterator");
        Iterator itr = vector.iterator();
        while (itr.hasNext()) {
          System.out.println(itr.next());
        }
    }
}
Note : In the above example, we have used iterator with while loop to iterate Vector

5) Enumeration

We can use enumeration to loop through vector elements

  1. import java.util.*;
  2.  
  3. public class VectorEnumeration {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.         vector.add(14);
  7.         vector.add(7);
  8.         vector.add(21);
  9.         vector.add(28);
  10.         System.out.println("Vector elements are: ");
  11.         Enumeration en = vector.elements();
  12.         while(en.hasMoreElements()){
  13.           System.out.println(en.nextElement());
  14.          }
  15.     }
  16. }
import java.util.*;

public class VectorEnumeration {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
        vector.add(14);
        vector.add(7);
        vector.add(21);
        vector.add(28);
        System.out.println("Vector elements are: ");
        Enumeration en = vector.elements();
        while(en.hasMoreElements()){
          System.out.println(en.nextElement());
         }
    }
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on