Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Principles.NET: System.Collections System.Collections.Generics Generics Data Structures and Collections."— Presentation transcript:

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

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

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

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 IList Interface 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) structural properties

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. Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages.Add("Tom", 15); ages["Ann"] = 28; int a = (int) ages["Ann"]; create 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 ICollection IList LinkedList IDictionary List Dictionary SortedDictionary Index able Array-based Balanced search tree Hashtabel (key, value) -pair

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 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 demos\hashTest

13 Opgave Undersøg System.Collections.Generics


Download ppt "Principles.NET: System.Collections System.Collections.Generics Generics Data Structures and Collections."

Similar presentations


Ads by Google