一集合框架

上传人:乐*** 文档编号:115795187 上传时间:2019-11-14 格式:PPT 页数:67 大小:699KB
返回 下载 相关 举报
一集合框架_第1页
第1页 / 共67页
一集合框架_第2页
第2页 / 共67页
一集合框架_第3页
第3页 / 共67页
一集合框架_第4页
第4页 / 共67页
一集合框架_第5页
第5页 / 共67页
点击查看更多>>
资源描述

《一集合框架》由会员分享,可在线阅读,更多相关《一集合框架(67页珍藏版)》请在金锄头文库上搜索。

1、第一章集合框架学习目标熟悉Java2之前的容器类的继承关系,熟练使用其中的类和接口。熟悉Java集合框架,熟练使用其中的相关类和接口。熟练使用集合类的泛型用法。Java2之前的容器类在Java2之前,标准的Java类库只能为最有用的数据结构提供很小的一组类,我们通常称之为“容器类”。容器类是由“两条线”组成。Enumeration接口Enumeration接口是贯穿于整个Java2之前容器类的“迭代器”接口,定义了可以枚举(遍历)容器中元素的方法,只能单方向运动。成员方法:publicbooleanhasMoreElements()publicEnextElement()范例解析对于Enume

2、ration接口来说,有一种通常的写法,如:while(en.hasMoreElements()Objectobj=en.nextElement()其它操作集合框架概述java.util包中包含了Java最强大的子系统之一,这就是“集合框架”。“集合框架”是在Java2初始版本中增加进来的,Java2版本1.4和1.5中增强了它的功能。一个集合就是一组对象的容器。集合的加入导致了java.util中许多元素在构造和体系结构方面的根本变化,它也扩展了能够应用的任务域。所以说,集合是所有Java程序员都需要密切关注并熟练掌握的一种技术。集合类层次关系Iterator接口Iterator接口是集合框

3、架中的单向迭代器。成员方法:publicbooleanhasNext()publicEnext()publicvoidremove()ListIterator接口ListIterator是Iterator接口的子接口,是集合框架中的双向迭代器。成员方法:publicbooleanhasPrevious()publicEprevious()publicvoidadd(Eobj)publicvoidset(Eobj)publicintnextIndex()publicintpreviousIndex()Collection接口-1Collection接口是集合框架的基础,声明了多数集合类都有的核心

4、方法,因为多数的集合都实现Collection接口,熟悉它的方法对清楚理解集合框架是非常必要的。成员方法:publicIteratoriterator()publicbooleanadd(Eo)publicbooleanaddAll(Collectionc)Collection接口-2成员方法:publicbooleanremove(Objecto)publicbooleanremoveAll(Collectionc)publicbooleanretainAll(Collectionc)publicvoidclear()publicintsize()publicbooleanisEmpty()

5、publicbooleanequals(Objecto)Collection接口-3成员方法:publicbooleancontains(Objecto)publicbooleancontainsAll(Collectionc)其它方法参照API文档。List结构层次关系List接口-1成员方法:publicListIteratorlistIterator()publicListIteratorlistIterator(intindex)publicvoidadd(intindexEelement)publicbooleanaddAll(intindexCollectionc)publicEs

6、et(intindexEelement)publicEget(intindex)List接口-2成员方法:publicListsubList(intfromIndexinttoIndex)publicintindexOf(Objecto)publicintlastIndexOf(Objecto)Vector类-1在编程中,经常用数组来对一些数据进行操作,不仅编程方便,而且效率高,但是,数组必须是定长的,其元素类型必须一致,不能满足动态增元素和存放不同类型的元素的需要。java.util包中的Vector类正是用来处理这种情况的。构造方法:publicVector()Vector类-2构造方法:

7、publicVector(intinitialCapacity)publicVector(intinitialCapacityintcapacityIncrement)publicVector(Collectionc)成员方法:publicintcapacity()publicintsize()Vector类-3成员方法:publicvoidaddElement(Eobj)publicvoidinsertElementAt(Eobjintindex)publicvoidsetElementAt(Eobjintindex)publicbooleanremoveElement(Objectobj)

8、publicvoidremoveElementAt(intindex)publicvoidremoveAllElements()Vector类-4成员方法:publicEfirstElement()publicElastElement()publicEelementAt(intindex)publicbooleanisEmpty()publicbooleancontains(Objectelem)publicintindexOf(Objectelem)publicintindexOf(Objectelemintindex)Vector类-5成员方法:publicintlastIndexOf(O

9、bjectelem)publicintlastIndexOf(Objectelemintindex)publicEnumerationelements()publicbooleanequals(Objecto)publicObjectclone()publicvoidtrimToSize()publicvoidsetSize(intnewSize)Vector类-6上述方法都是在Java2之前的容类中出现的方法,因已将Vector纳入集合框架,所以之前的方法也进行了泛化处理。练习:(1)构造一个Vector对象,存储10个String对象和10个Integer对象,随机存,要求输入所有Inte

10、ger对象封装值的和、所有String对象连接一起的值。(2)构造一个Vector对象,存储10个Number对象,然后用泛型方法进行迭代、求和。(分别用Enumeration和Iterator完成迭代操作)Stack类-1Stack类是实现了标准的后进先出的堆栈,是Vector的子类,但是它的下标是从1开始的(栈顶下标为1,栈底下标为size)。所以说Vector类所用到的方法Stack的方法都可以用,但是在实际应用中,尽量不使用继承来的方法。构造方法:publicStack()Stack类-2成员方法:publicEpush(Eitem)publicEpop()publicEpeek()p

11、ublicbooleanempty()publicintsearch(Objecto)练习:构造一个Stack对象,测试入栈出栈操作。ArrayList类ArrayList是底层以数组实现的List。构造方法:publicArrayList()publicArrayList(intinitialCapacity)publicArrayList(Collectionc)成员方法:publicvoidtrimToSize()练习:创建一个ArrayList对象,测试添加元素、单向遍历及双向遍历。CopyOnWriteArrayList类ArrayList的一个线程安全的变体,其中所有可变操作(添加

12、、设置,等等)都是通过对基础数组进行一次新的复制来实现的。构造方法:publicCopyOnWriteArrayList()publicCopyOnWriteArrayList(Collectionc)publicCopyOnWriteArrayList(EtoCopyIn)LinkedList类-1LinkedList是底层以双向链表实现的List。构造方法:publicLinkedList()publicLinkedList(Collectionc)成员方法:publicvoidaddFirst(Eo)publicvoidaddLast(Eo)LinkedList类-2成员方法:publi

13、cEgetFirst()publicEgetLast()publicEremoveFirst()publicEremoveLast()练习:创建一个LinkList,添加、插入、删除元素的测试。比较器Comparable接口Comparable接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序,类的compareTo方法被称为它的自然比较方法。成员方法:publicintcompareTo(To)比较器Comparator接口比较方法强行对某些对象Collection进行整体排序。可以将Comparator传递给sort方法从而允许在排序顺序上实现精确控制。还可以使用Com

14、parator来控制某些数据结构(如TreeSet或TreeMap)的顺序。成员方法:publicintcompare(To1To2)publicbooleanequals(Objectobj)Queue结构层次关系Queue接口Queue代表的是先进先出的队列,有如下行为:成员方法:publicbooleanoffer(Eelement)publicEremove()publicEpoll()publicEpeek()publicEelement()范例解析importjava.util.publicclassLinkedListQueueTestpublicstaticvoidmain(S

15、tringargs)Queueq=newLinkedList()q.offer(1)q.offer(2)Iteratoriter=q.iterator()while(iter.hasNext()Integeri=iter.next范例解析System.out.println(i)while(q.peek()!=null).OR.!q.isEmpty()Integeri=q.poll()System.out.println(i)BlockingQueue接口-1Queue接口有七个具体实现的子类,其中实现了BlockingQueue接口的五个“阻塞队列类”和两个“非阻塞队列类”(Priority

16、Queue和ConcurrentLinkedQueue)。成员方法:publicintdrainTo(Collectionc)publicintdrainTo(CollectioncintmaxElements)BlockingQueue接口-2成员方法:publicbooleanoffer(EolongtimeoutTimeUnitunit)throwsInterruptedExceptionpublicEpoll(longtimeoutTimeUnitunit)throwsInterruptedExceptionpublicvoidput(Eo)throwsInterruptedExceptionpublicEtake()throwsInterruptedExceptionpublicintremainingCapacity()BlockingQueue接口-3BlockingQueue接口五个实现类分别是:(1)ArrayBlockingQueue(2)LinkedBlockingQueue(3)PriorityBlockingQueue

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号