当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
计算机二级辅导:Java中Collection、List的使用
发布时间:2010/3/13 9:14:58 来源:城市学习网 编辑:MOON
  List list = new ArrayList();
  String str = "hello";
  list.add(str);
  Integer integer =new Integer(1);
  list.add(integer);
  //取出list中的值
  String str2 = (String)list.get(0);
  System.out.println(str2);
  System.out.println(list.size());
  //iterator迭代器
  List intList = new ArrayList();
  for(int i=0;i10;i++)
  {
  intList.add("i"+i);
  }
  Iterator it = intList.iterator();
  while(it.hasNext()){
  String inte = (String)it.next();
  System.out.println(inte);
  }
  System.out.println("=for=");
  for(int j=0;jintList.size();j++)
  {
  System.out.println(intList.get(j));
  }
  Collection collection = new ArrayList();
  //向容器中加入对象
  String str = "Hello";
  collection.add(str);
  Integer integer = new Integer(1);
  collection.add(integer);
  Object obj = new Object();
  collection.add(obj);
  //返回容器长度
  //size返回collection中的元素数
  System.out.println("容器的长度:"+collection.size());
  //如何取对象?
  Object object = collection.toArray();
  //未进行类型转换
  System.out.println("第一个元素是:"+object[0]);
  //移除容器中制定的对象
  collection.remove(obj);
  //清空容器中的对象
  collection.clear();
  System.out.println("容器的长度:"+collection.size());
  :
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved