Java中的Vector elements()方法

Java中的Vector elements()方法

在Java的Vector类中, java.util.vector.elements() 方法用于获取Vector中存在的值的枚举。

语法:

Enumeration enu = Vector.elements()

参数: 该方法不需要参数。

返回值: 该方法返回Vector的值的枚举。下面的程序用于演示java.util.Vector.elements()方法的工作原理:

程序1:

// Java code to illustrate the elements() method
import java.util.*;
 
public class Vector_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>(5);
 
        // Inserting elements into the table
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
        vec_tor.add("Welcomes");
        vec_tor.add("You");
 
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating an empty enumeration to store
        Enumeration enu = vec_tor.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}

输出:

The Vector is: [Geeks, 4, Geeks, Welcomes, You]
The enumeration of values are:
Geeks
4
Geeks
Welcomes
You

程序2:

import java.util.*;
 
public class Vector_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>(5);
 
        // Inserting elements into the table
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(20);
        vec_tor.add(25);
        vec_tor.add(30);
 
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating an empty enumeration to store
        Enumeration enu = vec_tor.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        while (enu.hasMoreElements()) {
            System.out.println(enu.nextElement());
        }
    }
}

输出:

The Vector is: [10, 15, 20, 25, 30]
The enumeration of values are:
10
15
20
25
30

时间复杂度: O(n),其中n是Vector中元素的数量。

辅助空间: O(n)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程