- 1、本文档被系统程序自动判定探测到侵权嫌疑,本站暂时做下架处理。
- 2、如果您确认为侵权,可联系本站左侧在线QQ客服请求删除。我们会保证在24小时内做出处理,应急电话:400-050-0827。
- 3、此文档由网友上传,因疑似侵权的原因,本站不提供该文档下载,只提供部分内容试读。如果您是出版社/作者,看到后可认领文档,您也可以联系本站进行批量认领。
查看更多
Lecture Notes on Software Engineeringr(软件工程下)麻省理工学院课件
Lecture 14: Design Patterns for Traversal
October 8, 2002
In this lecture, we consider design patterns intended for traversing collections of objects.
14.1 Iterator
Suppose you are designing the interface for Set, a data type that represents a set of objects. We’ve already seen a
number of data structures that you might use to implement a set: an array, a linked list, a hash table, an ordered
tree (q.v. the Java library class TreeSet). The question is, what interface should we provide to clients of our
set that enables them to iterate over the members of the set, regardless of how the set is actually stored? In
pseudocode, the client wants to do this:
for each element in set
do action
There are a number of ways we might provide this functionality to clients of our Set class. We might provide a
method that gets an object at a given index:
// try #1
interface Set {
Object get (int i);
// returns: element at index i in some ordering of
// the set (fixed until the set is modified)
// throws: IndexOutOfBoundsException if i0 or i=size()
...
}
Clients could then iterate over all the indices from 0 up to the size of the set, getting each element. This interface
would be ideal if the set is stored in an array; implementation would be trivial and fast. For a linked list, however,
getting good performance would be far from trivial, since get(i) would have to count i elements down the list.
Iterating over all the elements in the list would take quadratic time. A clever optimization can bring this down to
linear time by caching the index and list entry of the last element accessed. But it doesn’t solve the problem for
the hash table representation of a set, or the tree representation. Worse, this interface gives the client the freedom
to jump around
您可能关注的文档
最近下载
- 校园欺凌预防与干预措施研究报告.docx VIP
- 2023年云南省劳动合同书电子版(5篇).doc VIP
- 医德医风考核表模板.pdf VIP
- 单证员招聘笔试题及解答(某大型国企).docx VIP
- 事业单位市场监督管理局面试真题及答案.docx VIP
- 中国血脂管理指南(基层版)2025ppt课件.pptx VIP
- “任务型”教学中应注意的几个问题.doc VIP
- 职业技术学院数字化校园项目策划用户需求书.doc VIP
- 【教学创新大赛】《分子生物学》教学创新成果报告.pdf VIP
- 质量管理工具及其应用指导之2:理解组织及其环境-质量管理体系及其过程:过程方法和策划类(雷泽佳编制2024A0).docx VIP
原创力文档


文档评论(0)