java 动态数组之vector与Enumeration实现

java不支持动态数组  用vector集合类来实现

 

Enumeration的两个方法

 

hasMoreElements () 测试此枚举是否包含更多的元素。

  nextElement () 如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。

 

package Bean;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

public class TestVector {
 
 
 public static void main(String[] args)
 {
  Vector v = new Vector();
  System.out.println("");
  int b = 0;
  while(true)
  {

   try {
    b = System.in.read();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if(b == '\r' || b == '\n')
    break;
   else
   {
    int num = b - '0';
    v.addElement(new Integer(num));
   }
   
   int sum = 0;
   Enumeration e  = v.elements();
   while(e.hasMoreElements())
   {
    Integer printByte = (Integer) e.nextElement();
    sum += printByte.intValue();
   }
   System.out.println(sum);   
  }

 }


}
 

你可能感兴趣的:(java,bean)