第6章集合框架.pptVIP

  • 6
  • 0
  • 约8.23千字
  • 约 60页
  • 2017-12-20 发布于天津
  • 举报
第6章集合框架.ppt

容器综述 (1)Collection,一个独立元素的序列。其中List按照元素的插进顺序保存元素;而set不能有重复元素。 (2)Map,一组键值对(key-value)对象的序列,可以使用key来查找value,其中key是不可以重复的,value可以重复。可以称其为字典或者关联数组。 1. Collection接口 (1)没有直接Collection接口的容器类 (2)实现的Collection接口的容器类,大多实现的是Collection的子接口——List 与 Set 子接口的实现限制了Collection接口的实现 表现为: List接口的容器类中元素有指标,可重复 Set接口的容器类中元素无指标,不可重复 Collection定义的通用方法 所有容器类都有四个方面的操作:增、删、改、查 针对单个元素 针对其它Collection 增: add(object), addAll(collection) 删: remove(object), removeAll(collection) retainAll(collection) 改: 缺失 查: contains(object), containsAll(collection) 迭代器 iterator() 代码6-8 容器类中的匹配 1. 匹配——相等 容器类会自动调用元素所属类,继承自Object类的equals方法。 重写equals方法 容器类中add,remove,contain…..方法用调用 最像数组的容器,每个对象有index的 List接口方法简介 Collection的子接口,有直接实现子类 增: add(object) addAll(collection) 删: remove(object) removeAll(collection) remover(index) 改: set(index, obj) 查: contains(object) containsAll(collection) get(index) 迭代器 iterator() listIterator() 代码6-9 ListAsArray ArrayList、Vector与LinkedList构造方法 public ArrayList(); public ArrayList(int initialCapacity); public ArrayList(Collection c); public Vector(); public Vector(int initialcapacity, int capacityIncrement); public Vector(int initialcapacity) public Vector(Collection c) public LinkedList (); public LinkedList (Collection c); List的三个实现类ArrayList,Vector, LinkedList 1. 它们可以通过其它Collection对象构造 2. 它们各有区别: ArrayList 非同步的,底层为数组的容器类 读快改慢 Vector 线程同步的动态数组,源于C++ 读快改慢 LinkedList 是Queue的实现,底层为链表。 读慢改快 代码6-10 Speed List容器的遍历 List a = new LinkedList(); 1. 容器大小的获取 int conSize = a.size(); 2. 利用指标 int index = 0 ; for(index = 0;indexconSize; index++){ Object o = a.get(index); } 小程序: 设计一个容器,存储教室里所有物品,并写遍历它。 数学中的集合,每个元素没有标签 Set接口 Collection的子接口,有直接实现子类,没有新增方法 不存equals为true的元素 增: add(object) addAll(collection) 删: remove(object) removeAll(collection) 改: 没有 查: contains(object) containsAll(collection) 代码6-11 TestSet Set接口的实现类HashSet, TreeSet HashSet底层是一个HashMap,进入对象equals为true时,hashCode()必需相等。 否则无法判定相同。 代码6-9 TestHashSet TreeSet一个有序的Set,进入的对象必需两两可比。 进入TreeSet的对象

文档评论(0)

1亿VIP精品文档

相关文档