收藏 分销(赏)

JAVA-第六章old.ppt

上传人:仙人****88 文档编号:14775097 上传时间:2026-11-19 格式:PPT 页数:44 大小:1.69MB 下载积分:10 金币
下载 相关
JAVA-第六章old.ppt_第1页
第1页 / 共44页
JAVA-第六章old.ppt_第2页
第2页 / 共44页


点击查看更多>>
资源描述
信息科学与工程学院 软件工程系,网络程序设计(Java语言),第6章 容器,6.1 容器的概念,学生信息管理系统,如何管理全部的学生对象?,用数组的缺点?,容器类:Java类库中提供的用于管理,对象集,的类,功能:,持有、操作对象,自动完成容量的变化,6.2 Java容器类框架,java.util包,Collection,列表(List):按照,一定次序,排列的对象集,对象之间有次序关系,对象,可以,重复,。,集合(Set):对象唯一,,不会重复,。,队列(Queue):根据排队规则确定对象的顺序,Map(映射表):一群成对的对象集,这些对象各自保持着“键-值”(key-value)对应关系。,允许使用一个对象来查找另一个对象(字典),重复:,两个对象之间equals()返回为true,6.3 Collection接口,int size();,boolean isEmpty();,boolean contains(Object o);,Iterator iterator();,Object toArray();,boolean add(E o);,boolean remove(Object o);,boolean containsAll(Collection c);,boolean addAll(Collection c);,boolean removeAll(Collection c);,boolean retainAll(Collection c);,void clear();,boolean equals(Object o);,实例1,Collection,c=new ArrayList();,c.add(Java);,c.add(new Student(1,Tom,60);,c.add(new Integer(12);,System.out.println(c.size();,System.out.println(,c,);,3,Java,Studentc17164,12,C的类型为何定义为Collection类型?,编写的大部分代码与接口打交道,并且唯一需要指定所使用的精确类型的地方是创建的时候,优点:如果要修改具体的接口实现,只需要改变创建对象的代码。,最后一句话如何理解?是如何实现的?,6.4 List接口,Collection接口的子接口,特点:实现该接口的类中的元素是,有顺序的,可以重复,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=new,LinkedList,();,for(int i=0;i=5;i+),list.add(a+i);,System.out.println(list);,list.,add,(3,b);,System.out.println(list);,list.,set,(6,c);,System.out.println(list);,System.out.println,(String)list.get(2);,System.out.println(list.,indexOf,(a3);,list.,remove,(1);,System.out.println(list);,System.out.println(list.set(6,c);,a0,a1,a2,a3,a4,a5,a0,a1,a2,b,a3,a4,a5,a0,a1,a2,b,a3,a4,c,a2,4,a0,a2,b,a3,a4,c,进入容器后成为Object,,取出的时候仍为Object,,因此要进行强制类型转换,实例1,实例2 泛型,List,list=,new,LinkedList,();,for,(,int,i=0;i2)i.remove();,System.out.println(c);,c.remove(s);,遍历的另一种方法:增强版的for循环(foreach)(JDK5.0),int arr=1,2,3,4,5;,for(int i:arr),System.out.println(i);,1,2,3,4,5,Collection c=new ArrayList();,c.add(new String(“java);,c.add(new String(“c+);,c.add(new String(c#);,for(Object o:c),System.out.println(o);,java,c+,c#,说明:,所有的Collection都可以使用foreach语法遍历,对容器只是向前遍历、不修改容器本身时使用,缺点:,对于数组:不能方便的访问数组下标,对于Collection,不能方便的删除元素,实现的内部是调用Iterator(锁定),说明:,除了简单遍历,不要使用增强的for循环,6.8.2 ListIterator,Iterator的子类型,功能更强大,只能用于,各种List,的访问,特点:可以双向移动,创建方法(来源于List),listIterator():产生一个指向List开始处的ListIterator,listIterator(n):创建一个开始指向列表索引为n的元素处的listIterator,List all=,new,ArrayList();,all.add(hello);,all.add(_);,all.add(world);,ListIterator iter=all.listIterator();,while,(iter.hasNext(),String str=(String)iter.next();,System.,out,.println(str+);,while,(iter.hasPrevious(),String str=(String)iter.previous();,System.,out,.println(str+);,hello,_,world,world,_,hello,双向的迭代,但此种输出方式只适用于List接口,例1,List all=,new,ArrayList();,all.add(hello);all.add(_);all.add(world);,ListIterator iter=all.listIterator();,while,(iter.hasNext(),String str=(String)iter.next();,System.,out,.print(str+);,iter.,set,(T-+str);,System.,out,.println();,System.,out,.println(Current is:+all);,iter.,add,(Last);,while,(iter.hasPrevious(),String str=(String)iter.previous();,System.,out,.print(str+);,System.,out,.println();,System.,out,.println(Current is:+all);,hello _ world,Current is:T-hello,T-_,T-world,Last T-world T-_ T-hello,Current is:T-hello,T-_,T-world,Last,例2,ListIterator可以在遍历过程中,修改,容器内容,List all=,new,ArrayList();,all.add(hello);,all.add(_);,all.add(world);,ListIterator iter=all.,listIterator(2),;,while,(iter.hasNext(),String s=(String)iter.next();,System.,out,.println(s);,world,例3,6.8.3 Enumeration接口,特点:与Iterator接口类似,但,只提供,了遍历Vector和HashTable(子类Properties)集合类型的功能,,不支持,集合元素的移除操作。,Enumeration接口是JDK 1.0时就推出的,是最早的迭代输出接口,最早使用Vector时就是使用Enumeration接口进行输出的。,Vector vec=new Vector();,vec.addElement(Kobe);,vec.addElement(James);,Enumeration enumeration=vec.,elements,();,while(enumeration.,hasMoreElements,(),String element=(String)enumeration.,nextElement,();,System.,out,.println(element);,思考:Vector能用Iterator遍历么?,6.9.1 Collections类,类,java.util.,Collections,提供了基于List容器排序的,静态,方法,void sort(List);/对List容器内元素,排序,void shuffle(List);/对List容器内的对象进行,随机,排序,void reverse(List);/对List容器内的对象进行,逆序,排列,void copy(List dest,List src);,/把src中List容器内容,拷贝,到dest List容器,int binarySearch(List,Object);,/对,顺序,的List容器,用折半查找方法,查找,指定对象,6.9 实用类,实例,List list1=new LinkedList();,for(int i=0;i=9;i+)list1.add(a+i);,System.out.println(list1);,Collections.shuffle,(list1);,System.out.println(list1);,Collections.reverse,(list1);,System.out.println(list1);,Collections.sort,(list1);,System.out.println(list1);,System.out.println(,Collections.binarySearch,(list1,a2);,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a7,a1,a5,a4,a8,a0,a9,a2,a6,a3,a3,a6,a2,a9,a0,a8,a4,a5,a1,a7,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,2,思考:为何sort能够排序?,对排序的对象有何要求?,容器中对象所属类要实现Comparable接口,6.9.2 Arrays类,功能:定义了多种,数组操作,方法,实现了对数组元素的排序、填充、转换为列表或字符串形式、增强的检索和深度比较等功能。,Integer a=3,25,12,79,48;,System.,out,.println(Arrays.,toString,(a);,Arrays.,sort,(a);,System.,out,.println(Arrays.,toString,(a);,int idx=Arrays.,binarySearch,(a,25);,System.,out,.println(idx);,List list=Arrays.,asList,(3,4,5);,System.,out,.println(list);,3,25,12,79,48,3,12,25,48,79,2,3,4,5,总结,数组将数字和对象联系起来。它保存类型明确的对象,查询对象时候,不需要对结果做类型转换。它可以是多维的。但是,数组一旦生成,其容量就不能改变。,Collection保存单一的元素,而Map保存相关联的键值对。有了Java的泛型机制,就可以指定容器中存放的对象类型,因此你就不会将错误类型的对象放置到容器中,并且从容器中获取元素时,不必进行类型转换。各种Collection和各种Map都可以在你向其中添加更多的元素时,自动调整其尺寸。容器不能持有基本类型,但是Wrapper包装机制会仔细地执行就基本类型到容器中所持有的包装器类型之间的自动转换。,和数组一样,List也能建立数字索引与对象的关联,因此,数组和List都是排好序的容器。List能自动扩充容量。,总结,如果要进行大量随机访问,使用ArrayList;如果要经常从表中间插入或删除元素,则使用LinkedList。,各种Queue和栈的行为,都使用LinkedList代替。,Map是一种将对象与对象相关联的设计。HashMap设计用来进行快速访问,而TreeMap保持“键”始终处于排序状态,所以没有HashMap快。LinkedHashMap保持元素插入的状态,但是也是通过散列提供了快速访问的能力。,总结,Set不接受重复元素。HashSet提供最快的查询速度。TreeSet保持元素处于排序状态。LinkedHashSet以插入顺序保存元素。,新程序中不应使用过时的Vector、HashTable和Stack。,
展开阅读全文

开通  VIP、SVIP  下载更划算
下载10份以上建议开通 VIP 会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        关注我们

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服