Vector iterator() in Java

iterator(): This method is available in java.util.Vector class of Java.

Syntax:

Iterator<K> java.util.Vector.iterator()

This method returns an iterator over the elements in this list in proper sequence.

Parameters: NA

Returns: an iterator over the elements in this list in proper sequence.

Exceptions: NA

Approach

Java

import java.util.Iterator;
import java.util.Vector;

public class Vectoriterator {
    public static void main(String[] args) {

        Vector<String> vector = new Vector<>();

        vector.add("Hello");
        vector.add("Java");
        vector.add("C++");
        vector.add("Program");

        Iterator<String> iterator = vector.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");

        }

    }
}

Output:

Hello Java C++ Program 

No comments:

Post a Comment