首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Java中Collection、List的使用

Java中Collection、List的使用

www.jz123.cn  2009-12-10   来源:   IT专家网    责任编辑(袁袁)    我要投递新闻

 Collection的使用


 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;i<10;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;j 
  {
  System.out.println(intList.get(j));
  }

  List的使用

 

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


上一篇:java GB转 UTF-8字符 下一篇:Java 获得文件编码格式的代码

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图