Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Collections

Similar presentations

Presentation on theme: "Data Structures and Collections"— Presentation transcript:

1 Data Structures and Collections
Principles .NET: System.Collections System.Collections.Generics Generics

2 Data Structures and Collections
Read and write (use) specifications Data structure and algorithms Choose and use an adt, e.g. Map application ADT class: HashMap TreeMap ---- class Appl{ ---- Map m; ----- m= new XXXMap(); interface: (e.g. Map) Specification Choose and use a data structure, e.g. TreeMap Know about

3 Overview Algorithms: Data structures: Abstract data types: search
sweep sorting divide & conquer recursion Data structures: static/dynamic array linked list trees: Search trees balanced hashing Abstract data types: lists/sequenses stack queue Table/Map/Dictionary Java-specific: Collections (generics) Collection Set List Map .NET-specific: Collections.Generics IDictionary IList

4 Collections Library System.Collections
Data structures in .NET are normally called Collections Are found in namespace System.Collections Compiled into mscorlib.dll assembly Uses object and polymorphism for generic containers. Deprecated! Classes: Array ArrayList Hashtable Stack Queue

5 Collection Interfaces
System.Collections implements a range of different interfaces in order to provide standard usage of different containers Classes that implements the same interface provides the same services Makes it easier to learn and to use the library Makes it possible to write generic code towards the interface Interfaces: ICollection IEnumerable IEnumerator IList IComparer IComparable

6 ArrayList ArrayList stores sequences of elements.
duplicate values are ok – position- (index-) based Elements are stored in an resizable array. Implements the IList interface public class ArrayList : IList, IEnumerable, ... { // IList services ... // additional services int Capacity { get... set... } void TrimToSize() int BinarySearch(object value) int IndexOf (object value, int startIndex) int LastIndexOf (object value, int startIndex) } control of memory in underlying array searching

7 read/write existing element
IList Interface 9. The FCL January 2003 IList defineres sequences of elements Access through index public interface IList : ICollection { int Add (object value); void Insert(int index, object value); void Remove (object value); void RemoveAt(int index); void Clear (); bool Contains(object value); int IndexOf (object value); object this[int index] { get; set; } bool IsReadOnly { get; } bool IsFixedSize { get; } } add new elements remove containment testing read/write existing element (see comment) C# supports class members called "indexers", which you see above: object this[int index] { get; set; ] Indexers are properties with an array-like syntax: collection[index] = value; // call to set .NET makes heavy use of indexers… structural properties Denmark .NET Workshop © 2003 Joe Hummel 7

8 Hashtable Hashtable supports collections of key/value pairs
keys must be unique, values holds any data stores object references at key and value GetHashCode method on key determine position in the table. create Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages.Add("Tom", 15); ages["Ann"] = 28; int a = (int) ages["Ann"]; add update retrieve

9 Hashtable Traversal Traversal of Hashtable
each element is of type DictionaryEntry (struct) data is accessed using the Key and Value properties Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages["Tom"] = 15; foreach (DictionaryEntry entry in ages) { string name = (string) entry.Key; int age = (int) entry.Value; ... } enumerate entries get key and value

10 .NET 2: System.Collections.Generics
(key, value) -pair ICollection<T> IList<T> LinkedList<T> IDictionary<TKey, TValue> List<T> SortedDictionary<TKey, TValue> Dictionary <TKey, TValue> Index able Array-based Balanced search tree Hashtabel

11 Forskellige implememteringer
ArrayList vs. LinkedList: ArrayList: Direkte access på index Statisk memory allokering Bøvlet indsæt og slet i midten LinkedList: Dynamisk memory allokering Kun sekventiel adgang Se eksempel: demos\lists

12 Forskellige implememteringer
HashTable vs. SortedDictionary: Hashtable: Statisk memory allokering Meget hurtig i gennemsnit (konstant, men memory overhead)) Meget dårlig i worst case (linær) Nondeterministisk SortedDictionary: Dynamisk memory allokering Meget hurtig i worst case (logaritmisk) Se eksempel: demos\hashTest

13 Opgave Undersøg System.Collections.Generics

Download ppt "Data Structures and Collections"

Similar presentations

Ads by Google