當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.elements()用法及代碼示例


描述

這個elements()方法用於返回此向量的組件的枚舉。返回的 Enumeration 對象將在此向量中的相似索引位置生成所有項目。

聲明

以下是聲明java.util.Vector.elements()方法

public Enumeration<E> elements()

參數

  • 不接受任何輸入參數

返回值

它返回此向量的組件的枚舉。

異常

NA

示例

下麵的例子展示了 java.util.Vector.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<Integer>(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 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.util.Vector.elements() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。