1 / 13

Data Structures and Collections

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.

Download Presentation

Data Structures and Collections

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. Data Structures and Collections Principles .NET: System.Collections System.Collections.Generics Generics

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

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

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

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

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

  13. Opgave • UndersøgSystem.Collections.Generics

More Related