Java program & tutorial to read all elements in a vector by using iterator. Here, we have discussed the various methods to do the aforementioned using sample program & Example. Compiler has been added so that you can execute the programs yourself, alongside suitable examples and sample outputs. To travel through a Vector, we can use any of the following options:
- Iterator
- Enumeration
- List Iterator
- Object[]
- SpecificType[]
- As if a Normal Array
What Is Iterator
Iterator is a special mechanism that allows to iterate (loops/travels) through a set of elements. All Collection objects hold a set of elements.
So java provides a common mechanism to travel through any Collection so that we can perform some operations on each of the elements. Generally an iterator (object) travels from first element to last element in a sequence.
The java.util.Iterator interface can be used to travel through a Vector (and other Collections).
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); Iterator<String> vi=v1.iterator(); while(vi.hasNext()) { System.out.println(vi.next()); } } } |
Output:
1 2 3 |
USA UK UAE |
The iterator() method of Vector gives an Iterator object. There was some internal mechanism to implement the Iterator interface.
So we need not to worry about which class is implementing the Iterator interface. The Iterator object initially refers the starting element.
We can take the next element using the method next(). We can check whether we have any more elements remaining in the Vector or not using the method hasNext().
What Is Enumeration
Similar to Iterator, we can use java.util.Enumeration interface to travel through a Vector (and other Collections). The elements() method of Vector gives a reference to Enumeration to travel through.
The hasMoreElements() can be used to check we have any more elements in the Collection. The nextElement() can be used to take the next available element.
If we travel beyond the existing elements, a NoSuchElementException will be raised.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"hello"); v1.add(1,"welcome to"); v1.add(2,"JAVAPROGRAMSHUB"); Enumeration<String> ve=v1.elements(); while(ve.hasMoreElements()) { System.out.println(ve.nextElement()); } } } |
Output:
1 2 3 |
hello welcome to JAVAPROGRAMSHUB |
List Iterator
The java.util.ListIterator is a sub interface of Iterator. So it automatically has the methods hasNext() and next() discussed above in the Iterator way.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); ListIterator<String> vli=v1.listIterator(); while(vli.hasNext()) { System.out.println(vli.next()); } } } |
Output:
1 2 3 |
USA UK UAE |
Along with these methods the ListIterator has hasPrevious(), previous(), nextIndex(), previousIndex(), etc. As the method names indicate, we can travel both sides. In the following example, we are travelling both sides of the Vector.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); ListIterator<String> vli=v1.listIterator(); while(vli.hasNext()) System.out.println(vli.next()); while(vli.hasPrevious()) System.out.println(vli.previous()); } } |
Output:
1 2 3 4 5 6 |
USA UK UAE UAE UK USA |
Object[]
The toArray() method on Vector() returns an array of Objects. Once we get the elements into the Object array, we just can travel through the array to work on them.
When we take the Vector data into the array the modifications made on Vector will not be effective on the Array because the system creates a new copy of data for the array. This can be seen in the following example.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); Object[] va=v1.toArray(); for(int i=0;i<va.length;i++) System.out.println(va[i]); v1.set(1,"Britain"); System.out.println(v1.get(1)); // Britain System.out.println(va[1]); // UK } } |
Output
1 2 3 4 5 |
USA UK UAE Britain UK |
SpecificType[]
Instead of taking the elements into a more common Object[], we can take into a specific type of array using an overloaded version of toArray() on Vector.
This version of toArray() takes an array of the required type as argument and returns such an array. The elements can be seen in both argumented array as well as receiver.
We should make sure that the argumented array has sufficient references to hold hashcodes of the resulting elements. The target array (receiver from method) will be created by the system automatically.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); String[] vsa1=new String[v1.size()]; // vsa1 is argumented array String[] vsa2=v1.toArray(vsa1); // vsa2 is receiving array (target array) for(int i=0;i<vsa1.length;i++) System.out.println(vsa1[i]); for(int i=0;i<vsa2.length;i++) System.out.println(vsa2[i]); v1.set(1,"Britain"); System.out.println(v1.get(1)); System.out.println(vsa1[1]); // modifications on original Vector are not reflected here System.out.println(vsa2[1]); // modifications on original Vector are not reflected here } } |
Output:
1 2 3 4 5 6 7 8 9 |
USA UK UAE USA UK UAE Britain UK UK |
As if a Normal Array
We can read all elements of a Vector without using any Iterator. We just can go through the Vector as if we are travelling in an array. For this we can use get() method on the Vector for each element. The get() method takes index of the element as argument and returns the element.
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; class Check { public static void main(String arg[]) { Vector<String> v1=new Vector<String>(); v1.add(0,"USA"); v1.add(1,"UK"); v1.add(2,"UAE"); for(int i=0;i<v1.size();i++) { System.out.println(v1.get(i)); } } } |
Output:
1 2 3 |
USA UK UAE |