100 likes | 260 Views
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
E N D
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.LengthTells 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.
ArrayLists can change size, but are less efficient [new System.Collections.ArrayList]Makes an ArrayList [x.Add object]Adds a new element object to the end of ArrayList x [x.Insert position object]Adds a new element object to x at position [x.Remove object]Removes the element object from x, wherever it occurs [x.RemoveAt position] [x.Clear]Removes all elements from x «Some results omitted to save space» ► [define mylist [new System.Collections.ArrayList]] [ ] ► [mylist.Add 1] ► [mylist.Add 2] ► [mylist.Add 3] ► mylist [1 2 3] ► [mylist.Remove 2] ► mylist [1 3] ► [mylist.Insert 1 “test”] ► mylist [1 “test” 3] ► The ArrayList: a more powerful list type
Stacks can only be changes at their “top” or beginning [new System.Collections.Stack]Makes a new stack [s.Push object]Adds a new object to the “top” of the stack s [s.Pop]Removes the item at the top of s and returns it [s.Peek]Returns the item at the top of s without removing it [s.Clear]Removes all data from s ► [define s [new System.Collections.Stack]] ‹System.Collections.Stack› ► [s.Push 1] ► [s.Push 2] ► [s.Push 3] ► [s.Peek] 3 ► [s.Peek] 3 ► [s.Pop] 3 ► [s.Pop] 2 ► [s.Pop] 1 ► [s.Pop] Error: Stack empty. The stack: a simpler collection class
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
[new System.Collections.Hashtable]Makes a new hashtable [lookup dict key] Returns the object listed in dict under key Or null if key doesn’t appear in dict [store-dictionary dict key value]or: [[lookup dict key] ← value] Adds or changes the entry for key in dict to value [dict.Clear]Removes all entries from dict [dict.Contains key]Checks whether dict contains an entry for key ► [define h [new Hashtable]] ► [lookup h “test”] null ► [[lookup h “test”] ← “got it”] "got it" ► [[lookup h “test2”] ← “something different”] "something different" ► [lookup h “test”] "got it" ► [[lookup h “test”] ← “changed it”] "changed it" ► [lookup h “test”] "changed it" The Hashtable class
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