1 / 10

four

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

dmitri
Download Presentation

four

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. four container classes

  2. 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

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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

  9. [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

  10. 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

More Related