java 动态数组之vector与Enumeration实现

本文介绍Java中Vector集合类的使用方法,并通过一个具体示例演示如何利用Enumeration遍历Vector中的元素并进行求和运算。

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);   
  }

 }


}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值