Lecture Notes on Software Engineeringr(软件工程下)麻省理工学院课件.pdf

Lecture Notes on Software Engineeringr(软件工程下)麻省理工学院课件.pdf

  1. 1、本文档被系统程序自动判定探测到侵权嫌疑,本站暂时做下架处理。
  2. 2、如果您确认为侵权,可联系本站左侧在线QQ客服请求删除。我们会保证在24小时内做出处理,应急电话:400-050-0827。
  3. 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

文档评论(0)

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

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

1亿VIP精品文档

相关文档