Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Positions of Items in a List

As shown in Program gif, objects that implement the Cursor interface can be used to access, insert, and delete objects in an ordered list. Program gif defines an inner class  called OrderedListAsArray.MyCursor that implements the Cursor interface. The idea is that instances of this inner class are used by the OrderedListAsArray class to represent the abstraction of a position in an ordered list.

   program8811
Program: OrderedListAsArray.MyCursor class.

The MyCursor class has a single field, offset, that is used to record an offset in the array of objects. A single constructor is provided which simply assigns a given value to the offset field. Program gif also defines the getDatum method of the MyCursor class. This method simply returns the item in the array at the position record in the offset field, provided that position is valid. The running time of the getDatum method is simply O(1).


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.