📜  Java中的向量迭代器()方法与示例

📅  最后修改于: 2022-05-13 01:55:05.781000             🧑  作者: Mango

Java中的向量迭代器()方法与示例

存在于Java.util 包中的 Vector 类的iterator() 方法用于返回与 Vector 相同元素的迭代器。元素从向量中的内容以随机顺序返回。

句法:

Iterator iterate_value = Vector.iterator();

参数:该函数不带任何参数。

返回类型:该方法迭代向量的元素并返回值(迭代器)。

示例 1:

Java
// Java code to illustrate iterator() Method
// of Vector class
 
// Importing required classes
import java.util.*;
import java.util.Vector;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector of string type
        Vector vector = new Vector();
 
        // Adding elements into the Vector
        // using add() method
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("4");
        vector.add("Geeks");
 
        // Printing and displaying the Vector
        System.out.println("Vector: " + vector);
 
        // Now creating an iterator
        Iterator value = vector.iterator();
 
        // Display message only
        System.out.println("The iterator values are: ");
 
        // Condition holds true till there is single element
        // remaining using hasNext() method
        while (value.hasNext()) {
 
            // Displaying the values
            // after iterating through the vector
            System.out.println(value.next());
        }
    }
}


Java
// Java code to illustrate iterator() method usage
// with usage of hashCode() method
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector of integer type
        Vector vector = new Vector();
 
        // Adding custom elements into the Vector
        // using add() method
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
        vector.add(50);
 
        // Displaying the Vector
        System.out.println("Vector: " + vector);
 
        // Creating an iterator
        Iterator value = vector.iterator();
 
        // Display message for better readability
        System.out.println("The iterator values are: ");
 
        // Holds true till there is single element
        while (value.hasNext()) {
 
            // Printing the values after iterating
            // through the vector
            // using next() method
            System.out.println(value.next());
        }
    }
}


输出:
Vector: [Welcome, To, Geeks, 4, Geeks]
The iterator values are: 
Welcome
To
Geeks
4
Geeks

示例 2:

Java

// Java code to illustrate iterator() method usage
// with usage of hashCode() method
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector of integer type
        Vector vector = new Vector();
 
        // Adding custom elements into the Vector
        // using add() method
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
        vector.add(50);
 
        // Displaying the Vector
        System.out.println("Vector: " + vector);
 
        // Creating an iterator
        Iterator value = vector.iterator();
 
        // Display message for better readability
        System.out.println("The iterator values are: ");
 
        // Holds true till there is single element
        while (value.hasNext()) {
 
            // Printing the values after iterating
            // through the vector
            // using next() method
            System.out.println(value.next());
        }
    }
}
输出:
Vector: [10, 20, 30, 40, 50]
The iterator values are: 
10
20
30
40
50