12、容器解析.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
12、容器解析

Set方法举例: Set s=new HashSet(); s.add(hello); s.add(world); s.add(new Integer(100)); s.add(hello);//相同元素不会被加入 System.out.println(s); 输出结果:[hello, 100, world] 关于new Name(f1,f2);由于重写过所以是相同的所以加上2个重复的也只有1个。 List接口 List接口是Collection的子接口,实现List接口的容器类中的元素是有顺序的,而且可以重复。 List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。 JDK所提供的List容器类有ArrayList,LinkedList等。 Object get(int index); Object set(int index,Object element); void add(int index,object element); object remove(int index); int indexOf(object o); int lastIndexOf(object o); List 方法举例 List l1=new LinkedList(); for(int i=0;i=5;i++){ l1.add(a+i);} System.out.println(l1); l1.add(3, a100); System.out.println(l1); l1.set(6, a200); System.out.println(l1); System.out.print(l1.get(2)+ ); System.out.println(l1.indexOf(a3)); l1.remove(1);System.out.println(l1); * LOGO 容器 第五章 第五章 容器 容器的概念:为什么要使用容器? 阅读如下程序: public class Test{ public static void main(String[] args){ Name name1=new Name(“n1”); Name name2=new Name(“n2”) Name name3=new Name(“n3”) } } 第五章 容器 容器API J2SDK所提供的容器API位于java.util包内。 容器API的类图结构如下图所示: interface Collection interface Set HashSet interface List LinkedList ArrayList interface Map HashMap 第五章 容器 Collection接口-定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。 Set中的数据对象没有顺序且不可重复。 List中的数据对象有顺序且可以重复。 Map接口定义了存储“键(key)—值(value)映射对”的方法。 第五章 容器 Collection接口中所定义的方法: int size(); //装了多少个元素 boolean isEmpty(); //是不是为空 void clear(); //清空 boolean contains(Object element); //是不是包含element对象..equlse boolean add(Object element); //添加 boolean remove(Object element); //去除 Iterator iterator(); Boolean containsAll(Collection c); //还不是包含集合中所有元素 第五章 容器 boolean addAll(Collection c); //添加所有 boolean removeAll(Collection c); //去除所有 boolean retainAll(Coolection c); //求当前和目前集合的交集 Object[] toArray(); //把对象转换成对象的数组 第五章 容器 Collection方法举例: Collection c=new ArrayList(); //可以放入不同类型的对象 c.add(hello); c.add(new Name(f1,l1)); c.add(new Integer(100)); System.out.p

文档评论(0)

此项为空 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档