four
container classes
Container classes
An important area of computer science is the study of
data structures
Roughly: how we put data objects together to make them useful
A very important kind of data structure is the container
A kind of object that “holds” a collection of other objects
There are many kinds of containers
Most of the container classes we’ll be using are
subtypes of List
Lists
Have a bunch of elements
Have them in a definite order
You can ask them how many elements they
have
You can ask for an element at a specific position
You can change the element at a specific
position
Basic list operations
[length list] or: list.Length
Tells you how many items are in the list
[get list position]
Returns the element of list at position
[[get list position] ← new-value]
Changes the element of list at position
The simplest list type: the array
An array is a fixed-length list
The [list args …] procedure returns arrays
Arrays are very fast and efficient
There are specialized kinds of arrays that only allow
certain types of elements, but we mostly won’t deal with
these this quarter.
The ArrayList: a more powerful list type
ArrayLists can change size, but are less efficient «Some results omitted to save
space»
[new System.Collections.ArrayList] ► [define mylist
Makes an ArrayList [new
System.Collections.ArrayList]]
[]
[x.Add object] ► [mylist.Add 1]
Adds a new element object to the end of ► [mylist.Add 2]
ArrayList x ► [mylist.Add 3]
[x.Insert position object] ► mylist
Adds a new element object to x at position [1 2 3]
► [mylist.Remove 2]
► mylist
[x.Remove object] [1 3]
Removes the element object from x, wherever ► [mylist.Insert 1 “test”]
it occurs ► mylist
[x.RemoveAt position] [1 “test” 3]
►
[x.Clear]
Removes all elements from x
The stack: a simpler collection class
Stacks can only be changes at their “top” or ► [define s [new
System.Collections.Stack]]
beginning ‹System.Collections.Stack›
► [s.Push 1]
► [s.Push 2]
[new System.Collections.Stack] ► [s.Push 3]
Makes a new stack ► [s.Peek]
[s.Push object] 3
Adds a new object to the “top” of the stack s ► [s.Peek]
3
[s.Pop] ► [s.Pop]
Removes the item at the top of s and returns 3
it ► [s.Pop]
[s.Peek] 2
► [s.Pop]
Returns the item at the top of s without 1
removing it ► [s.Pop]
[s.Clear] Error: Stack empty.
Removes all data from s
Dictionary classes
Dictionaries are objects that store
associations between pairs of objects
One object is called the key
The other the value
The most common dictionary class is the
Hashtable
The Hashtable class
[new System.Collections.Hashtable] ► [define h [new Hashtable]]
Makes a new hashtable ► [lookup h “test”]
null
[lookup dict key] ► [[lookup h “test”] ← “got it”]
Returns the object listed in dict under key "got it"
Or null if key doesn’t appear in dict ► [[lookup h “test2”] ←
“something different”]
"something different"
[store-dictionary dict key value] ► [lookup h “test”]
or: [[lookup dict key] ← value]
"got it"
Adds or changes the entry for key in dict to value
► [[lookup h “test”] ←
“changed it”]
[dict.Clear] "changed it"
Removes all entries from dict ► [lookup h “test”]
[dict.Contains key] "changed it"
Checks whether dict contains an entry for key
Next time
We’ll have an elaborate example based on
Myst/Zork-like adventure game
Which you will extend for you second
assignment
The first assignment will be up tonight or
tomorrow