Latest :

How to Read All Elements In Vector By Using Iterator

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:

  1. Iterator
  2. Enumeration
  3. List Iterator
  4. Object[]
  5. SpecificType[]
  6. 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:


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:


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:


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:



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:



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:


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:



Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...