Java容器类学习教程.pptx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《Java容器类学习教程.pptx》由会员分享,可在线阅读,更多相关《Java容器类学习教程.pptx(35页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、容器的概念容器 APICollection 接口Iterator 接口Set 接口List 接口 和 Comparable 接口Map 接口本章内容第1页/共35页Collection 表示一组对象,它是集中,收集的意思,就是把一些数据收集起来。Collection函数库是在java.util 包下的一些接口和类,类是用来产生对象存放数据用的,而接口是访问数据的方式。Collection函数库与数组的两点不同:数组的容量是有限制的,而Collection库没有这样的限制,它容量可以自动的调节。Collection函数库只能用来存放对象,而数组没有这样的限制。Collection接口是Colle
2、ction层次结构 中的根接口,它定义了一些最基本的访问方法,让我们能用统一的方式通过它或它的子接口来访问数据。区别:Collection代表一组对象,Collection函数库就是java中的集合框架,Collection接口,是这个集合框架中的根接口。1.存放在Collection 库中的数据,被称为元素(element)。Collection的概念第2页/共35页Collections Framework Hierarchy Including ClassesCollectionHashSet(Set)LinkedListVector,ArrayList(List)HashtableHa
3、shmap(Map)TreeSet(SortedSet)TreeMap(SortedMap)第3页/共35页Collection 接口:定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。Set 中的数据对象没有顺序且不可以重复。List 中的数据对象有顺序且可以重复。Map 接口定义了存储“键(key)值(value)映射对”的方法。Collections Framework API第4页/共35页Collection 接口Collection 接口中所定义的方法:boolean add(Object element);boolean remove(Object eleme
4、nt);boolean contains(Object element);int size();boolean isEmpty();void clear();Iterator iterator();boolean containsAll(Collection c);boolean addAll(Collection c);boolean removeAll(Collection c);boolean retainAll(Collection c);Object toArray();第5页/共35页Collection 方法举例import java.util.*;public class Co
5、llectionTest public static void main(String args)Collection c=new ArrayList();/可以放入不同类型的对象c.add(hello);c.add(new Boolean(true);c.add(new Integer(100);System.out.println(size+c.size()+:+c);System.out.println(contains:+c.contains(new Integer(100);System.out.println(c.remove(new Boolean(true);System.ou
6、t.println(isEmpty:+c.isEmpty();System.out.println(size+c.size()+:+c);输出结果:size3:hello,true,100contains:truetrueisEmpty:falsesize2:hello,100第6页/共35页Collection 方法举例public class Person private int id;private String name;public Person(int id,String name)this.id=id;this.name=name;public int getId()return
7、 id;public String getName()return name;public void setId(int id)this.id=id;public void setName(String name)this.name=name;public String toString()return id:+id+|name:+name;第7页/共35页Collection 方法举例import java.util.*;public class CollectionTest1 public static void main(String args)Collection c=new Hash
8、Set();c.add(new Person(1,“c+);c.add(new Person(2,“java);System.out.println(c.size()+:+c);System.out.println(contains:+c.contains(new Person(2,java);System.out.println(c.remove(new Person(2,java);System.out.println(c.size()+:+c);输出结果:2:id:2|name:java,id:1|name:c+contains:falsefalse2:id:2|name:java,id
9、:1|name:c+第8页/共35页Collection 方法举例public int hashCode()final int PRIME=31;int result=1;result=PRIME*result+id;result=PRIME*result+(name=null)?0:name.hashCode();return result;public boolean equals(Object obj)if(this=obj)return true;if(obj=null)return false;if(getClass()!=obj.getClass()return false;fin
10、al Person other=(Person)obj;if(id!=other.id)return false;if(name=null)if(other.name!=null)return false;else if(!name.equals(other.name)return false;return true;lCollection类对象在调用remove、contains 等方法时需要比较对象是否相等,这会涉及到对象类型的 equals 方法和hashCode方法;对于自定义的类型,需要重写equals 和 hashCode 方法以实现自定义的对象相等规则。l注意:Java中规定,两
11、个内容相同的对象应该具有相等的 hash codes。l 增加Person类的equals和hashCode方法如下:第9页/共35页课堂练习(10分钟)熟悉Collection用法!第10页/共35页Iterator 接口所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象。Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。Iterator接口定义了如下方法:boolean hasNext();/判断是否有元素没有被遍历Object next();/返回游标当前位置的元素并将游标移动到下一个位置void rem
12、ove();/删除游标左面的元素,在执行完next之后该 /操作只能执行一次游标Next()元素第11页/共35页Iterator 方法举例import java.util.*;public class IteratorTest public static void main(String args)Collection c=new ArrayList();c.add(new Integer(1);c.add(new Integer(2);c.add(new Integer(3);c.add(new Integer(4);Iterator it=c.iterator();while(it.ha
13、sNext()/next()的返回值为Object类型,需要转换为相应类型Object tem=it.next();System.out.println(tem.intValue()+);输出结果:1 2 3 4 第12页/共35页Iterator 方法举例import java.util.*;public class IteratorTest1 public static void main(String args)Collection c=new ArrayList();c.add(good);c.add(morning);c.add(key);c.add(happy);for(Itera
14、tor it=c.iterator();it.hasNext();)String tem=(String)it.next();if(tem.trim().length()=3)it.remove();System.out.println(c);输出结果:good,morning,happy第13页/共35页Set 接口是Collection接口的子接口,Set接口没有提供额外的方法,Set接口的特性是容器类中的元素是没有顺序的,而且不可以重复。Set 容器可以与数学中“集合”的概念相对应。J2SDK API中 所提供的 Set 容器类有 HashSet,TreeSet 等。Set 接口第14页
15、/共35页Set 方法举例import java.util.*;public class SetTest public static void main(String args)Set s=new HashSet();s.add(hello);s.add(world);s.add(new Integer(4);s.add(new Double(1.2);s.add(hello);/相同的元素不会被加入System.out.println(s);输出结果:4,hello,1.2,world第15页/共35页Set 方法举例import java.util.*;public class TreeS
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 容器 学习 教程
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内