Data Structures and Collections

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

description

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. - PowerPoint PPT Presentation

Transcript of Data Structures and Collections

Principles .NET:

System.CollectionsSystem.Collections.Generics

Generics

Data Structures and Collections

Data Structures and Collections

interface:

(e.g. Map)

Specification

class Appl{

----

Map m;

-----

m= new XXXMap();

application

class:

HashMap

TreeMap

----

ADT Data structure and algorithmsChoose

and use an adt, e.g. Map

Choose and use a data structure, e.g.

TreeMap

Know about

Read and write (use)

specifications

Overview

Abstract data types:lists/sequensesstackqueueTable/Map/Dictionary

Java-specific:Collections (generics)Collection

SetList

Map

.NET-specific:Collections.GenericsIDictionaryIList

Algorithms:searchsweepsortingdivide & conquerrecursion

Data structures:static/dynamicarraylinked listtrees:

Search treesbalanced

hashing

Collections LibrarySystem.Collections

Data structures in .NET are normally called CollectionsAre found in namespace System.CollectionsCompiled into mscorlib.dll assemblyUses object and polymorphism for generic containers.Deprecated!Classes:ArrayArrayListHashtableStackQueue

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 servicesMakes it easier to learn and to use the libraryMakes it possible to write generic code towards the interface

Interfaces:ICollectionIEnumerableIEnumeratorIListIComparerIComparable

ArrayList

ArrayList stores sequences of elements.duplicate values are ok – position- (index-) basedElements 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 memoryin underlying array

searching

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

Hashtable

Hashtable supports collections of key/value pairskeys must be unique, values holds any datastores object references at key and valueGetHashCode 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 Hashtableeach 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

ICollection<T>

IList<T> LinkedList<T> IDictionary<TKey, TValue>

List<T>Dictionary

<TKey, TValue>SortedDictionary<TKey, TValue>

Index ableArray-based

Balanced search tree Hashtabel

(key, value) -pair

Forskellige implememteringer

ArrayList vs. LinkedList:ArrayList:

Direkte access på indexStatisk memory allokeringBøvlet indsæt og slet i midten

LinkedList:Dynamisk memory allokeringKun sekventiel adgang

Se eksempel:demos\lists

Forskellige implememteringer

HashTable vs. SortedDictionary:Hashtable:

Statisk memory allokeringMeget hurtig i gennemsnit (konstant, men memory overhead))Meget dårlig i worst case (linær)Nondeterministisk

SortedDictionary:Dynamisk memory allokeringMeget hurtig i worst case (logaritmisk)

Se eksempel:demos\hashTest

Opgave

Undersøg

System.Collections.Generics