objects, classes, and hierarchies 2 container classes - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

objects, classes, and hierarchies 2 container classes

Description:

[using System.Collections] Access collection classes [new ArrayList] Makes an ArrayList ... all data from s [define s [new Stack]] 'System.Collections.Stack' [s. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 11
Provided by: ianhor2
Category:

less

Transcript and Presenter's Notes

Title: objects, classes, and hierarchies 2 container classes


1
objects, classes, and hierarchies 2container
classes
2
Types and subtypes
Object
  • Types often have subtypes
  • Integers are a subtype of numbers
  • Boxes are a subtype of Pictures
  • All types are subtypes of the magic type Object
  • Subtyping means objects can have many types
  • All Integers are also Numbers
  • All Boxes are also Pictures
  • And all objects are Objects

List
Number
Array
Integer
Float
Picture
Box
Line
Group
3
Example 2The container class hierarchy
Collection
  • 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 well be using are
    subtypes of List
  • All the collection classes in .NET (and so in
    Meta) are in the namespace System.Collections

List
Dictionary
Array
ArrayList
Stack
Hashtable
Object String
4
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

5
Basic list operations
  • length list or list.CountTells you how
    many items are in the list
  • item list positionReturns the element of list
    at position
  • set-item list position new-valueor item
    list position ? new-valueChanges the element
    of list at position

6
The simplest list type the array
  • An array is a fixed-length list
  • Very fast and efficient
  • Cant add/remove element after creation
  • Although you can change elements
  • The list args procedure returns an array
  • Technically, the type Object
  • Which means an array of arbitrary objects
  • There are other kinds of specialized arrays too

7
The ArrayList a more powerful list type
  • ArrayLists can change size, but are less
    efficient
  • using System.CollectionsAccess collection
    classes
  • new ArrayListMakes an ArrayList
  • x.Add object
  • Add object to end of ArrayList x
  • x.Insert position object
  • Adds element at the specified position
  • x.Remove object
  • Removes all occurrences of object
  • x.RemoveAt position
  • Remove element at position
  • x.ClearRemoves all elements from x
  • Some results omitted to save space
  • ?using System.Collections
  • ?define mylist new ArrayList
  • ? mylist.Add 98
  • ? mylist.Add 38
  • ? mylist.Add 4
  • ? mylist
  • 98 38 4
  • ? mylist.Remove 38
  • ? mylist
  • 98 4
  • ? mylist.Insert 1 test
  • ? mylist
  • 98 test 4
  • ?

8
The stack a simpler collection class
  • Stacks can only be changes at their top or
    beginning
  • new StackMakes a new stack
  • s.Push objectAdds a new object to the top of
    the stack s
  • s.PopRemoves the item at the top of s and
    returns it
  • s.PeekReturns the item at the top of s without
    removing it
  • s.ClearRemoves all data from s
  • ? define s new 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.

9
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

10
The Hashtable class
  • new HashtableMakes a new hashtable
  • lookup dict key
  • Returns the object listed in dict under key
  • Or null if key doesnt appear in dict
  • store-dictionary dict key valueor lookup
    dict key ? value
  • Adds or changes the entry for key in dict to
    value
  • dict.ClearRemoves all entries from dict
  • dict.Contains keyChecks whether dict contains
    an entry for key
  • ? define h new Hashtable
  • ? lookup h test
  • null
  • ? store-dictionary h test
    got it
  • "got it"
  • ? store-dictionary h test2 something
    different
  • "something different"
  • ? lookup h test
  • "got it"
  • ? store-dictionary h test
    changed it
  • "changed it"
  • ? lookup h test
  • "changed it"
Write a Comment
User Comments (0)
About PowerShow.com