📜  使用Java枚举通过 Vector 进行枚举

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

使用Java枚举通过 Vector 进行枚举

在Java Enumeration 类中,所有列出的常量默认都是 public、static 和 final。现在,在创建 Vector 之后,如果我们想通过 Vector 进行枚举,那么首先,我们必须获得 Vector 元素的 Enumeration,为此,我们使用了 elements() 方法。该方法是Java .util.Vector 类的成员函数。 elements()方法返回对实现Java.util.Enumeration 类的对象的引用,因此我们可以使用 hasMoreElements() 和 nextElement() 方法来帮助我们枚举 Vector。

宣言

public Enumeration elements()

句法:

Enumeration enu = Vector.elements()

参数:该方法不带任何参数。

返回值:该方法返回 Vector 值的枚举

Method

Returns 

hasMoreElements()If there exist more elements in the Enumeration then it returns true otherwise returns false. 
nextElement()If there exists any next element in the Enumeration then it returns that element.

示例 1:

Java
// Java program to Enumerate through a Vector 
  
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
    public static void main(String[] args) {
        
        // Creating an object of Vector which contains
          // String type elements
        Vector vector = new Vector<>();
  
        // Adding values to the Vector
        vector.add("Keep");
        vector.add("Calm");
        vector.add("and");
        vector.add("learn");
        vector.add("from");
        vector.add("GFG");
  
        // Displaying the values of the vector
        System.out.println("The elements of the Vector is : "
                           + vector);
  
        // Creating the Enumeration of the Vector elements.
        Enumeration enumeration = vector.elements();
  
        // Now Enumerating through the Vector and 
          // printing each enumeration constant.
        System.out.println(
          "The output after Enumerating through the Vector : ");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}


Java
// Java program to Enumerate through a Vector 
  
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
    public static void main(String[] args) {
        // Creating an object of Vector which contains 
        // double type elements
        Vector vector = new Vector<>();
  
        // Adding values to the Vector
        vector.add(1.2636);
        vector.add(23.0258);
        vector.add(266.1125);
        vector.add(2548.0125);
        vector.add(2154.02415);
        vector.add(856.012);
  
        // Displaying the values of the vector
        System.out.println("The elements of the Vector is : " 
                            + vector);
  
        // Creating the Enumeration of the Vector elements.
        Enumeration enumeration = vector.elements();
  
        // Now Enumerating through the Vector and printing 
        // each enumeration constant.
        System.out.println(
                    "The output after Enumerating through the Vector : ");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}


Java
// Java program to Enumerate through a Vector 
  
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
    public static void main(String[] args) {
        // Creating an object of Vector which contains 
        // elements of different data types
        Vector vector = new Vector<>();
  
        // Adding values to the Vector
        vector.add("Let's");
        vector.add("Contribute");
        vector.add("to");
        vector.add('G');
        vector.add('F');
        vector.add('G');
        vector.add(3);
        vector.add(12.054574);
  
        // Displaying the values of the vector
        System.out.println("The elements of the Vector is : "
                             + vector);
  
        // Creating the Enumeration of the Vector elements.
        Enumeration enumeration = vector.elements();
  
        // Now Enumerating through the Vector and printing 
        // each enumeration constant.
        System.out.println(
            "The output after Enumerating through the Vector : ");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}

输出
The elements of the Vector is : [Keep, Calm, and, learn, from, GFG]
The output after Enumerating through the Vector : 
Keep
Calm
and
learn
from
GFG

示例 2:

Java

// Java program to Enumerate through a Vector 
  
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
    public static void main(String[] args) {
        // Creating an object of Vector which contains 
        // double type elements
        Vector vector = new Vector<>();
  
        // Adding values to the Vector
        vector.add(1.2636);
        vector.add(23.0258);
        vector.add(266.1125);
        vector.add(2548.0125);
        vector.add(2154.02415);
        vector.add(856.012);
  
        // Displaying the values of the vector
        System.out.println("The elements of the Vector is : " 
                            + vector);
  
        // Creating the Enumeration of the Vector elements.
        Enumeration enumeration = vector.elements();
  
        // Now Enumerating through the Vector and printing 
        // each enumeration constant.
        System.out.println(
                    "The output after Enumerating through the Vector : ");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}
输出
The elements of the Vector is : [1.2636, 23.0258, 266.1125, 2548.0125, 2154.02415, 856.012]
The output after Enumerating through the Vector : 
1.2636
23.0258
266.1125
2548.0125
2154.02415
856.012

示例 3:

Java

// Java program to Enumerate through a Vector 
  
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
    public static void main(String[] args) {
        // Creating an object of Vector which contains 
        // elements of different data types
        Vector vector = new Vector<>();
  
        // Adding values to the Vector
        vector.add("Let's");
        vector.add("Contribute");
        vector.add("to");
        vector.add('G');
        vector.add('F');
        vector.add('G');
        vector.add(3);
        vector.add(12.054574);
  
        // Displaying the values of the vector
        System.out.println("The elements of the Vector is : "
                             + vector);
  
        // Creating the Enumeration of the Vector elements.
        Enumeration enumeration = vector.elements();
  
        // Now Enumerating through the Vector and printing 
        // each enumeration constant.
        System.out.println(
            "The output after Enumerating through the Vector : ");
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }
}
输出
The elements of the Vector is : [Let's, Contribute, to, G, F, G, 3, 12.054574]
The output after Enumerating through the Vector : 
Let's
Contribute
to
G
F
G
3
12.054574