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