130 likes | 262 Views
Data Structures and Collections. Principles . NET: System.Collections System.Collections.Generics Generics. Choose and use a data structure, e.g. TreeMap. Data Structures and Collections. Read and write (use) specifications. Data structure and algorithms.
E N D
Data Structures and Collections Principles .NET: System.Collections System.Collections.Generics Generics
Choose and use a data structure, e.g. TreeMap 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 Know about
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
Collections LibrarySystem.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
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
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
IList Interface IListdefineres 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
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
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
.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
Forskelligeimplememteringer • ArrayList vs. LinkedList: • ArrayList: • Direkte access på index • Statisk memory allokering • Bøvletindsæt og sletimidten • LinkedList: • Dynamisk memory allokering • Kun sekventieladgang • Se eksempel:demos\lists
Forskelligeimplememteringer • HashTable vs. SortedDictionary: • Hashtable: • Statisk memory allokering • Megethurtigigennemsnit (konstant, men memory overhead)) • Megetdårligi worst case (linær) • Nondeterministisk • SortedDictionary: • Dynamisk memory allokering • Megethurtigi worst case (logaritmisk) • Se eksempel:demos\hashTest
Opgave • UndersøgSystem.Collections.Generics