第七章 Collection 集合框架课件.ppt

  1. 1、本文档共23页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第七章 Collection 集合框架课件

第七章 Collection 集合框架;简介;简介; ; ; ;7.3 List Collection--List--Vector Collection--List--ArrayList Collection--List--LinkedList ; 使用两种 List 实现的哪一种取决于你特定的需要。如果要支持随机访问,而不必在除尾部的任何位置插入或除去元素,那么,ArrayList 提供了可选的集合。但如果,你要频繁的从列表的中间位置添加和除去元素,而只要顺序的访问列表元素,那么,LinkedList 实现更好。;面试题目: What is the main difference between an ArrayList and a Vector? What is the main difference between Hashmap and Hashtable? So which is better? As a general rule, prefer ArrayList/Hashmap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collection API‘s external synchronization facility as shown below to temporarily synchronize your collections as needed: Map myMap = Collections.synchronizedMap (myMap); List myList = Collections.synchronizedList (myList); Java arrays are even faster than using an ArrayList/Vector and perhaps therefore may be preferable. ArrayList/Vector internally uses an array with some convenient methods like add(..), remove(…) etc.;7.3.1 LinkedList类 LinkedList类的内部结构是线性链表,所以它适合从列表中间添加删除元素,而随机访问元素的效率较低。LinkedList类添加了一些处理列表两端元素的方法。; 7.3.2 ArrayList类   ArrayList类封装了一个动态再分配的Object[]数组。所以ArrayList适合随机访问内部元素,而在列表中间添加删除元素的效率较低。ArrayList的常见方法都在List接口中。 例子: public static void main(String args[]) { // 建立空列表 ArrayListString list = new ArrayListString (); // 在列表尾部加入元素 list.add(AA); list.add(BB); list.add(CC); // 通过索引遍历列表 for (int i = 0; i list.size(); i++) { // 列表中取出的对象都是Object类型,需要强制转化 String temp = (String) list.get(i); System.out.println(temp); } } ;;面试问题: 当Iterator 运行时,是否能改原来的collection: What is an Iterator? A: Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtaine

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档