Java Vector elements() 方法



描述

Java Vector elements() 方法用於返回此向量的元件的列舉。返回的 Enumeration 物件將生成此向量中所有相同索引位置的項。

宣告

以下是 java.util.Vector.elements() 方法的宣告

public Enumeration<E> elements()

引數

  • 不接受任何輸入引數

返回值

它返回此向量的元件的列舉。

異常

獲取列舉以迭代整數向量元素示例

以下示例顯示了 Java Vector elements() 方法的使用,以獲取此向量物件的元素的列舉。我們建立了一個整數向量,並使用 add() 方法向其中添加了一些元素。然後,我們透過使用 elements() 獲取列舉並對其進行迭代來迭代此向量的元素。

package com.tutorialspoint;

import java.util.Vector;
import java.util.Enumeration;

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

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);      

      // adding elements into the enumeration
      Enumeration<Integer> e = vec.elements();

      // let us print all the elements available in enumeration
      System.out.println("Numbers in the enumeration are :- "); 
      
      while (e.hasMoreElements()) {         
         System.out.println("Number = " + e.nextElement());
      }           
   }     
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Numbers in the enumeration are :- 
Number = 4
Number = 3
Number = 2
Number = 1

獲取列舉以迭代字串向量元素示例

以下示例顯示了 Java Vector elements() 方法的使用,以獲取此向量物件的元素的列舉。我們建立了一個字串向量,並使用 add() 方法向其中添加了一些元素。然後,我們透過使用 elements() 獲取列舉並對其進行迭代來迭代此向量的元素。

package com.tutorialspoint;

import java.util.Vector;
import java.util.Enumeration;

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

      // create an empty Vector vec with an initial capacity of 4      
      Vector<String> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add("D");
      vec.add("C");
      vec.add("B");
      vec.add("A");      

      // adding elements into the enumeration
      Enumeration<String> e = vec.elements();

      // let us print all the elements available in enumeration
      System.out.println("Elements in the enumeration are :- "); 
      
      while (e.hasMoreElements()) {         
         System.out.println("element = " + e.nextElement());
      }           
   }     
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Elements in the enumeration are :- 
element = D
element = C
element = B
element = A

獲取列舉以迭代物件向量元素示例

以下示例顯示了 Java Vector elements() 方法的使用,以獲取此向量物件的元素的列舉。我們建立了一個 Student 物件的向量,並使用 add() 方法向其中添加了一些元素。然後,我們透過使用 elements() 獲取列舉並對其進行迭代來迭代此向量的元素。

package com.tutorialspoint;

import java.util.Vector;
import java.util.Enumeration;

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

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Student> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(new Student(1, "Julie"));
      vec.add(new Student(2, "Robert"));
      vec.add(new Student(3, "Adam"));
      vec.add(new Student(4, "Jane"));

      // adding elements into the enumeration
      Enumeration<Student> e = vec.elements();

      // let us print all the elements available in enumeration
      System.out.println("Elements in the enumeration are :- "); 
      
      while (e.hasMoreElements()) {         
         System.out.println("element = " + e.nextElement());
      }           
   }     
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Elements in the enumeration are :- 
element = [ 1, Julie ]
element = [ 2, Robert ]
element = [ 3, Adam ]
element = [ 4, Jane ]
java_util_vector.htm
廣告

© . All rights reserved.