Introduction to OOP Chapter 19: Container Classes : next previous audio real text

Collection Traversal

Here is another difficult problem. How to you allow access to elements of a collection without exposing the internal structure?

Consider the conventional solution:

var
	aList : List;	(* the list being manipulated *)
	p : Link;	(* a pointer for the loop *)

begin
	...
	p := aList.firstLink;
	while (p <> nil) do begin
		writeln (p.value);
		p := p^.nextElement;
	end;
Needed to expose the link type, and the field nextElement.
Can we avoid this problem?
Intro OOP, Chapter 19, Slide 12