Java对象容器

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

对象容器(集合)数组虽然功能强大,但欠缺灵活性(长度不可改变)为什么要使用集合(对象容器)呢?InterfaceCollectionInterfaceList(列表)InterfaceSet(集合)AbstractCollectionAbstractSetAbstractListEnumSetLinkedHashSetAbstractSequentialListHashSetTreeSetArrayListVectorLinkedList所在的程序包:java.util.*Collection接口中定义的常用方法方法说明add(Objecto)在集合中添加指定对象remove(Objecto)从集合中移除指定对象clear()移除集合中的所有元素size()返回表示集合中当前元素数量的整数iterator()返回一个iterator对象用于遍历集合中的元素Set接口实现中不能包含重复元素。元素顺序不确定。集合中没有按索引位置访问元素的相应机制。Set接口的特点HashSet类(Set接口)一个集合类集合类的特点:无序、不重复,,,集合中可以存放不同类型的对象,,,所在包:java.util.*publicinterfaceSet{}publicclassHashSetimplementsSet{属性声明方法声明}HashSet类的常用方法publicHashSet(){}作用:创建一个空集合对象Publicvoidadd(Objecto){}作用:如果o对象内容不存在,向集合中添加元素oPublicvoidclear(){}作用:从集合中删除所有元素PublicbooleanisEmpity(){}作用:如果集合为空返回truePublicvoidremove(Objecto){}作用:从集合中删除指定元素oPublicintsize()作用:返回集合中元素的个数Publicbooleancontains(Objecto)作用:如果集合中含有o返回trueHashSet举例(无序、不重复)importjava.util.*;importjava.io.*;publicclasstestHashSet{publicstaticvoidmain(Stringargs[]){HashSetset_one=newHashSet();Stringx1=newString(china);Characterx2=newCharacter(':');Integerx3=newInteger(14);Stringx4=newString(billion)Stringx5=newString(china);set_one.add(x1);set_one.add(x2);set_one.add(x3);set_one.add(x4);set_one.add(x5);System.out.println(set_one);[billion,china,14,:]intsize;set_one.remove(x5);//虽然x5没有加入但集合中含有与x5相同的内容System.out.println(thelength:“+set_one.size());System.out.println(set_one);set_one.remove(x3);System.out.println(set_one);System.out.println(set_one.contains(x3));set_one.clear();System.out.println(set_one.isEmpty()++set_one.size());上段程序结果:billion,china,14:列表类(List接口)publicinterfaceList{}常见的列表类:ArrayList类、Vector类列表类的特点:将不同类型的对象按照一定的顺序组织,且同一对象可以多次插入效率高,多用于查询初步认识List集合:ArrayList简介ArrayList是一个数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。ArrayList实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输ArrayList中的操作不是线程安全的!所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector或CopyOnWriteArrayList。ArrayList源码透析ArrayList实际上是通过一个数组去保存数据(Object)的。当我们构ArrayList时;若使用默认构造函数,则ArrayList的默认容量大小是10.当ArrayList容量不足以容纳全部元素时,ArrayList会重新设置容量:新的容量=“(原始容量x3)/2+1.常见ArrayList遍历方法效率E(索引序号访问)E(Iterator方法遍历),E(for_each语句)LinkedList简介:LinkedList基于链表的数据结构,它也可以被当作堆栈、队列或双端队列进行操作.LinkedList实现java.io.Serializable接口,这意味着LinkedList支持序列化,能通过序列化去传输.LinkedList没有同步方法。如果多个线程同时访问一个List,则必须自己实现访问同步.LinkedList源码透析LinkedList实际上是通过双向链表去实现的,它包含一个非常重要的内部类:Entry。Entry是双向链表节点所对应的数据结构,它包括的属性有:当前节点所包含的值,上一个节点,下一个节点。常见LinkedList遍历方法效率E(for_each语句)E(Iterator方法遍历)E(索引序号访问)所在包:java.util.*publicclassArrayListimplementsList,…{属性声明方法声明}ArrayList类的常用方法构造函数:publicArrayList(){}作用:创建一个空列表对象publicArrayList(intinitCapacity){}作用:创建一个可容纳…个得空列表对象方法:Publicvoidadd(Objecto){}作用:在列表的末尾添加元素oPublicvoidadd(intindex,Objecto){}作用:在列表的index位置添加元素opublicObjectget(intindex){}作用:返回index位置的元素对象PublicintindexOf(objecto){}作用:找出列表中第一次出现对象o的位置PublicintlastIndexOf(objecto){}作用:找出列表中对象o出现的最后位置voidremoveRange(intbegin,intend){}作用:从列表中将序号从begin-end的元素删除publicobject[]toArray(){}作用:得到一个数组,数组元素是列表中元素ArrayList举例:publicclasstestArrayList{publicstaticvoidmain(Stringargs[]){ArrayListArrayL1=newArrayList(5);Stringx0=newString(china);Characterx1=newCharacter(':');Integerx2=newInteger(14);Stringx3=newString(billion);Stringx4=newString(china);ArrayL1.add(x0);ArrayL1.add(x1);ArrayL1.add(1,x2);ArrayL1.add(x3);ArrayL1.add(0,x4);System.out.println(ArrayL1);结果:china,china,14,:,billionSystem.out.println(x4isthe+ArrayL1.indexOf(x4)+element);System.out.println(thefirstelementis+ArrayL1.get(1).toString());Iteratort1=ArrayL1.iterator();try{while(t1.hasNext()){Stringx=String.valueOf(t1.next());System.out.println(x);}}catch(Exceptione){System.out.println(e.toString());}}}解释:Interator是一个Java定义的接口接口中含有下列方法:(1)hasNext()判断列表或集合中是否还有元素。(2)Next()从列表或集合中一次一次取对象。HashSet和ArrayList类均一个方法获得Interator接口方法:.iterator()importjava.util.*;classStudent{longID;StringfirstN;StringsecondN;Student(longID,StringfirstN,StringsecondN){this.ID=ID;this.firstN=firstN;this.secondN=secondN;}StringgetFullName(){returnfirstN+secondN;}}Set与ArrayList的比较Studentx=null;ArrayListcollection=newArrayList();Students1=newStudent(12345,John,Smith);Students2=newStudent(67890,Mike,Smith);Students3=newStudent(13579,Adam,Smith);collection.add(s1);collection.add(s1);collection.add(s2);collection.add(s3);Iteratort1=collection.iterator();try{while(t1.hasNext()){x=(Student)(t1.next());System.out.println(x.getFullName());}}catch(Exceptione){System.out.println(e.toString());}输出结果:JohnSmithJohnSmithMikeSmithAdamSmith创建HashSet实例代替ArrayList实例:Studentx=null;HashSetcollection=newHashSet();Students1=newStudent(12345,John,Smith);Students2=newStudent(67890,Mike,Smith);Students3=newStudent(13579,Adam,Smith);collection.add(s1);collection.add(s1);collection.add(s2);collection.add(s3);Iteratort1=collection.iterator();try{while(t1.hasNext()){x=(Student)(t1.next());System.out.println(x.getFullName());}}catch(Exceptione){System.out.println(e.toString());}输出结果:JohnSmithAdamSmithMikeSmithSet中

1 / 48
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功