集合类接口

集合类包括;: vector ,e

View Code
import java.util.*;
public class TestCollection {

    /**
     * Method main
     *
     *
     * @param args
     *
     */
    public static void main(String[] args) {
        Arraylist v=new Arraylist();
        System.out.println("please enter number:");
        while(true)
        {
           int b=System.in.read();
           //判断如果回车则字符输入完毕
         if(b=='\r'||b=='\n')
         {
               break;
         }
         else{
             int num=b-'0';// 这个地方需要关注下
             v.add(new Integer(num)) 
             v.addElement(new Integer(num)); //存储到vector中,用增加元素
             //需要的是对象,int不是对象,所以用Interger对象包装数组
       //addElement 是vector的方法   add()是arraylist的方法,其没有addElement方法
       
             }
        }
        int sum=0;
    Iterator e =    v.Iterator();// 返回enumeration对象
 // while(e.hasMoreElements())
 while(e.hasNext())
      {
      //    Inerger intobj=(Integer)e.nextElement();
      Inerger intobj=(Integer)e.next();
          sum+=intobj.intValue();
      }
          System.out.println(sum);
    // 没有对象时候
    //这个方法可以取出enumeration的所有整数对象,enumeration
    //有一个指示器,   返回指示器正指向的那个元素对象,然后把指示器指向
    //下一个元素对象  nextElement 是object型,需要转换
    }
}

 Enumeration ,vector,Arraylist,Collection,iterator,set,list

collection  各元素间没有指定的顺序,允许重复元素和多个null

set         

 

import java.util.*;
public class TestVector {

    /**
     * Method main
     *
     *
     * @param args
     *
     */
    public static void main(String[] args) {
        Vector v=new Vector();
        System.out.println("please enter number:");
        while(true)
        {
           int b=System.in.read();
           //判断如果回车则字符输入完毕
         if(b=='\r'||b=='\n')
         {
               break;
         }
         else{
             int num=b-'0';// 这个地方需要关注下
             v.addElement(new Integer(m)); //存储到vector中,用增加元素
             //需要的是对象,int不是对象,所以用Interger对象包装数组

             }
        }
        int sum=0;
    Enumeration e =    v.elements();// 返回enumeration对象
  while(e.hasMoreElements())
      {
          Integer intobj=(Integer)e.nextElement();
          sum+=intobj.intValue();
      }
          System.out.println(sum);
    // 没有对象时候
    //这个方法可以取出enumeration的所有整数对象,enumeration
    //有一个指示器,   返回指示器正指向的那个元素对象,然后把指示器指向
    //下一个元素对象  nextElement 是object型,需要转换
    }
}


 

转载于:https://www.cnblogs.com/xiao-wei-wei/archive/2012/06/02/2532036.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值